Simulink and .mat file

I'm trying to use a .mat function to call,run and then analyze a Simulink model.I can call and run, but then when I want to do a math operation to the outuput of Simulink it seems my .mat file can't load the data. I'm using a "save to workspace" block to save the data results from simulink.
Can anyone help me with this? Thanks

1 comentario

Andreas Goser
Andreas Goser el 27 de Feb. de 2011
From the words, I'd says you mean a MATLAB file, a function or script AKA M-File. MAT files are data files. Please clarify. I will write an answer based on the MATLA file assupmtion.

Iniciar sesión para comentar.

Respuestas (2)

Andreas Goser
Andreas Goser el 27 de Feb. de 2011

0 votos

The description fits to problems where a user runs simulations with the SIM command through a MATLAB code and wonders where the data ends up.
The To Workspace block writes into the MATLAB base workspace. Depending on hoe you work in MATLAB, you may use a different worksapce and thus don't find the data.
You shold also look at the SIM command and the option to retrieve your simulation results through the command.

4 comentarios

Frederico
Frederico el 27 de Feb. de 2011
First of all, thank you for the fast answer!
I think you got the questions right.
I can see the saved data results from Simulink in my "base" workspace,but as you say,my Matlab file it's not picking up these results.
This is how my .m file looks like.Maybe it's easier for you to see where the problem is.
"
function dtire = deflection(Chcomp,Clcomp,Chreb,Clreb,CHcomp,CLcomp,CHreb,CLreb)
find_system('Name','FST7DOF');
open_system('FST7DOF');
set_param('FST7DOF/Chcomp','value',Chcomp)
set_param('FST7DOF/Clcomp','value',Clcomp)
set_param('FST7DOF/Chreb','value',Chreb)
set_param('FST7DOF/Clreb','value',Clreb)
set_param('FST7DOF/CHcomp','value',CHcomp)
set_param('FST7DOF/CLcomp','value',CLcomp)
set_param('FST7DOF/CHreb','value',CHreb)
set_param('FST7DOF/CLreb','value',CLreb)
set_param(gcs,'SimulationCommand','Start');
N=length(dTireFR);
dtire = sqrt( 1/N*sum(dTireFR.^2));
"
The dTireFR is the name of the result variable saved to Workspace.
Andreas Goser
Andreas Goser el 27 de Feb. de 2011
I personally would resolve this by using the <http://www.mathworks.com/help/toolbox/simulink/slref/sim.html SIM command> instead of set_param...'Start'). SIM returns the simulation output.
Frederico
Frederico el 27 de Feb. de 2011
I will try you suggestion then I'll give you some feedback.
Frederico
Frederico el 27 de Feb. de 2011
Tried your suggestion on Matlab2008.
This is my code:
simOut=sim(FST7DOF,'Chcomp',1000,'Clcomp',2000,'Chreb',1000,'Clreb',2000,'CHcomp',1000,'CLcomp',2000,'CHreb',1000,'CLreb',2000);
And I get this error:
??? Error using ==> Simout at 1
'model' parameter must be a string.

Iniciar sesión para comentar.

Paulo Silva
Paulo Silva el 27 de Feb. de 2011

0 votos

The question has a bunch of errors and there isn't reliable information about what error occurs so I will try to guess:
1. If you just want to perform some math operations to the variable dTireFR in the workspace after the simulation has finished and there's some error message please copy and paste it in your question so we can see exactly what it is and in what line. The code you used is also welcome so we can help you. Now this is a wild guess, you are trying to do some calculations but you don't take into account the size and type of the variable, you could be also typing the name of the variable wrong.
2. In case you want to use functions that you made you must provide it as an argument like this myfunction(dTireFR) Another way but less recommended is to define dTireFR as global in the workspace and functions you want to use that variable. Yet another way, import the variable to your function like this assignin('caller','dTireFR',dTireFR) There are even more ways to do it but please do provide the variable as argument.

10 comentarios

Frederico
Frederico el 27 de Feb. de 2011
Hi Paulo,thanks for trying to help me.Where are you from?Your name sounds portuguese,would be easier for me to explain in portuguese.
I'm not trying to do calculations in the workspace,let's say in my matlab file,where I start the Simulation,at the end of the Sim I want to plot one of the results vs time.This is my code,
"
find_system('Name','FST7DOF');
open_system('FST7DOF');
set_param('FST7DOF/Chcomp','value','2000')
set_param('FST7DOF/Clcomp','value','1000')
set_param('FST7DOF/Chreb','value','2000')
set_param('FST7DOF/Clreb','value','1000')
set_param('FST7DOF/CHcomp','value','2000')
set_param('FST7DOF/CLcomp','value','1000')
set_param('FST7DOF/CHreb','value','2000')
set_param('FST7DOF/CLreb','value','1000')
set_param(gcs,'SimulationCommand','Start');
plot(tout,dTireFR)
"
The first time I run this (when the workspace is clean) it wont plot anything and shows this error:
??? Undefined function or variable 'tout'.
Error in ==> CallSimulink at 15
plot(tout,dTireFR)
The second time I run it it will do the graph,but I think it is based on the first simulation resutls.
Paulo Silva
Paulo Silva el 27 de Feb. de 2011
Before the plot and after sim insert this code:
tout = evalin('base', 'tout');
dTireFR = evalin('base', 'dTireFR');
Paulo Silva
Paulo Silva el 27 de Feb. de 2011
Forget my last message, I wasn't paying enough attention, you didn't used the sim function but you should because the sim function can return useful variables to your function. You can also change the simulation settings with simset and change it so it will always save the variables to you function instead of the worksapce
Paulo Silva
Paulo Silva el 27 de Feb. de 2011
Instead of set_param(gcs,'SimulationCommand','Start');
use [tout,x,dTireFR] = sim(gcs);
Frederico
Frederico el 27 de Feb. de 2011
I see in Matlab's Help that the function is [tout,x,yout1,...,yn],where "Each yi returns the output of the corresponding root-level Outport block for a model that has n such blocks."
this means I should replace the "save to workspace" block by a "Output" block?
I ran my function as you said ([tout,x,dTireFR] = sim(gcs);)but the dTireFR data doesnt have any data,it shows "[]" and also I get an x variable data with 14 columns...I supose 14 model outputs but don't know which variables they are related to.
Thanks,
Frederico
Paulo Silva
Paulo Silva el 27 de Feb. de 2011
Yes I forgot that you need to change the blocks, to workspace replaced by output block, you don't have any data because of the lack of one output block (that was my fault).
Frederico
Frederico el 27 de Feb. de 2011
Indeed.It works fine now.But I still have some questions about this.Even though I write [tout,x,dTireFR] the output block I'm using it's named as default "Out1" .Am I defining this way (including dTireFR in the [ brackets])the data from output 1 to be called dTireFR?
Paulo Silva
Paulo Silva el 27 de Feb. de 2011
You are accepting the data into the variable dTireFR, you can change the output block name to dTireFR (just to indicate to the user what's that block for).
Frederico
Frederico el 28 de Feb. de 2011
OK,so if I wanna operate with more data I suppose I just have to add more output blocks and do what I did with dTireFR, am I right?
What about the error I am getting since the beggining:
??? Error using ==> CallSimulink at 10
annotation does not have a parameter named 'value'.
I could find it is one of the set_param('Constant')...I was trying to figure out if there's any problem with the constant block in my Simulink but it seems everything is OK.Also if I set as comment this specific line (one of the 8 set_param(constant)), the Mat code will run without problems...
Any idea about it?
Paulo Silva
Paulo Silva el 28 de Feb. de 2011
[tout,x,y1,y2,y3...yn] = sim(gcs)
y1 is the variable that gets the data from the first output port
y2 is the variable that gets the data from the second output port
y3 is the variable that gets the data from the third output port
...
yn is the variable that gets the data from the nth output port
set_param('ModelName/ConstantBlockName','Value','2') works for me
set_param('ModelName/ConstantBlockName','value','2') also work good
You might have something wrong with the names, I don't know.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Feb. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by