Running simulink from matlab m file and then getting an output

Suppose I have simulink model called "A"and I run
simOut=sim('A');
from the script. How can I get time series of simulated variables?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Abr. de 2020
Editada: Walter Roberson el 26 de Abr. de 2020
Use set_param() on the appropriate blocks of the model to change the configuration of the model, and then sim() the model and record the results. For example,
gains = [1/10 1 2 5 10 50 100];
ngains = length(gains);
results = cell(ngains,1);
model_name = 'vdp';
block_name = 'vdp/Mu';
for K = 1 : ngains
set_param(block_name, 'Gain', sprintf('%f', gains(K)));
results{K} = sim(model_name);
end

10 comentarios

Thank you. To be more specific, I try to run
I did
simOut=sim('SIRcovid2018a');
and the output simOut does not record any variable other than tout. Please advise.
Thank you. I did
>simOut=sim('SIRcovid2018a','SaveState');
then it says
"The input arguments of a single output sim command should be model name followed by either a structure
with valid parameters as fields with corresponding values or a set of parameter name and parameter
value pairs"
out1 = sim('SIRcovid2018a', 'SaveState', 'on')
Thank you. This was not clear from the page you mentioned. It would be helpful if that page is edited to be clearer.
The output says index 1, 2, and 3. How can one name these variables when one defines the simulink model?
I do not know if there is a way to give individual names to the indices. You could always put in ToWorkspace blocks to save individual signals, or use signal logging. See the hints at https://www.mathworks.com/help/simulink/slref/sim.html#bvfe92n-3
But I assume there is a way to define variables when one set up these blocks in Simulink?
Signals are not the same as variables in Simulink.
You can name signals
And you can store values persistently by using workspace variables or data store. At the moment, I do not know what the differences are between using workspace variables compared to data store.
As an example, I have simulink model
then I want to understand how I can name the variables of the model so that it can be obtained as variables in Matlab.
if I recall correctly, that model does not have any variables. That model has states, and it has signals, and it has parameters.
If you are wanting to see final outcomes of signals then ToWorkspace. If you want to see how a signal evolved then log the signal.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 26 de Abr. de 2020

Comentada:

el 26 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by