Can Simscape models be build programmatically?

I want to build a Simscape model for thermal liquids programmatically.
sim_name='pipes';
if exist(sim_name,'file') == 4
% If it does then check whether it's open
if bdIsLoaded(sim_name)
% If it is then close it (without saving!)
close_system(sim_name,0)
end
% delete the file
delete([sim_name,'.mdl']);
end
new_system(sim_name)
y=50;
add_block(['Simscape/Foundation Library/Thermal Liquid/Elements/Pipe (TL)'], [sim_name,'/pipes'],'Position', [140+x 95 170+x 125]);
this throws the error There is no block named 'Simscape/Foundation Library/Thermal Liquid/Elements/Pipe (TL)'
Caused by:
Error using untitled8
Unable to load block diagram 'Simscape'
For blocks ordered under Simulink in the Library Browser, this problem does not occur.
Is there any way to build simscape models programmatically?

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 27 de Sept. de 2022

0 votos

I tried this and the block was added. To figure out for other blocks, open the library file, select the block and run "gcb" in MATLAB Command Window
add_block('fl_lib/Thermal Liquid/Elements/Pipe (TL)', ...)

3 comentarios

Claudius Rosendahl
Claudius Rosendahl el 28 de Sept. de 2022
Editada: Claudius Rosendahl el 28 de Sept. de 2022
Thank you! Adding the pipe works fine. I'm not sure I understand how to figure out the correct library directory for other blocks.
I don't understand what you mean by opening the library file. I know how to open the library browser in an extra window. But when I select a block in the library browser (light blue highlight) and run gcb (get current block, I suppose), it does not show the path of the block selected in the library browser, but the last selected block in my simulation, e.g. 'multi_pipes/pipes' when I had previously selected the block I created with
add_block('fl_lib/Thermal Liquid/Elements/Pipe (TL)', [sim_name,'/pipes'],'Position', [140+x 95 170+x 125]);
I tried using a new empty model, but the result is basically the same, i.e. I don't learn the library path.
Edit: I went to
C:\Program Files\MATLAB\R2022b\toolbox\physmod\simscape\simscape\m
And found the library simscape.slx. When I double-click on "Fluids" a new window opens and the header line reads SimscapeFluids_lib. This is the other library I'm using so now I'm satisfied. This seems like a rather cumbersome way to go about it and I think the solution you suggested is easier if applied correctly (which I failed to do). So I would still appreciate if you'd clarify.
For any readers wondering how I got to that file path: I used a normal file search in the MATLAB folder for fl_lib in hope of finding the library file @Fangjun Jiang was talking about. To add a block from this library, the code is something like this
add_block('SimscapeFluids_lib/Thermal Liquid/Pipes & Fittings/Pipe (TL)', [sim_name,'/pipes2'],'Position', [140+2*x 95 170+2*x 125]);
Cheers,
Claudius
Fangjun Jiang
Fangjun Jiang el 28 de Sept. de 2022
At the library browser, select any library, right click, select "Open ..."
awesome, thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Foundation and Custom Domains en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 27 de Sept. de 2022

Comentada:

el 28 de Sept. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by