Save Simulink Scope Data to figure through matlab code - matlab

As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I am using a load cell from which I gather data through UART and display it on a Matlab Simulink Scope. This works all fine. My next step was to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test cut into 1h “pieces” and to save the scope data to a figure each hour. So my Simulink simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
What I didn’t figure out:
Save the Simulink Scope Data to a figure through Matlab code, each time one increment in the loop ist done. I can get the figure from my Simulink Block but that’s not what I wanted (by that I mean that I literally get a screenshot from the Simulink Block; just to be clear).
for i=0:1:24
[…]
% Print to JEPG
fig = get_param('Simulink_Project','handle');
saveas(fig,sprintf('Figure%02d.jpeg',i ));
[…]
end
I hope that what I stated, was clear.
Thanks for your help.
Regards
hohmchri

Related

simulink simulation workspace output values do not change after running simulation from GUI m-file

I am trying to create a matlab gui that simulates a DC/DC converter, and I have succeeded in exporting data from gui to simulink in order to change signal attributes. The problem is that, when I change the values using the gui, the output signals on scopes in simulink change but the signals that I import from the simulation stay the same unless i re-run the simulation from simulink. I am using the evalin function to export workspace data to my m-file. I tried waiting for the simulation to end in order to solve the problem but it didn't.
I hope the question was clear enough and thank you in advance for your help !
Thank you but the problem resided in the command i used to run the simulation and not waiting the proper amount of time in order for the simulation to finish.
To resolve this problem I used the following:
open_system('nameofmodel.mdl')
set_param('nameofmodel', 'SimulationCommand', 'start')
while ~strcmp(get_param ('nameofmodel','SimulationStatus'),'stopped')
pause(1e-99);
end

Creating delay with a while loop in Matlab

As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I want to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test with a load cell cut into 1h “pieces” and to save the data to different sheets of an excel file each hour. So my simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
Between measuring steps I have to wait for simulink to finish its measures and saving the file in order for the Simulink window to be able to get closed by close_system('Thesis_SerDatTransm_Simulink').
So I tried to implement the delay with a while loop and by checking if the measures I get fit into an array of the size bigger than 449 (I measure 449 values):
for k=0:1:24
% Load Simulink
load_system('Thesis_SerDatTransm_Simulink.slx')
% Open Simulink
open_system('Thesis_SerDatTransm_Simulink.slx')
% Start Simulation
set_param('Thesis_SerDatTransm_Simulink', 'SimulationCommand', 'Start');
% Save Data
my_cell = sprintf('A%d',k);
xlswrite('file.xlsx',y,my_cell)
% Wait for Simulation
while 1
test=size(y)>=449;
if (test)
close_system('Thesis_SerDatTransm_Simulink')
break
end
end
end
The Problem now is, that program gets stuck at the while loop. Simulink is started, but no simulation or data gathering is done.
So I wondered if anyone could check if something is wrong with my While loop, since the rest of the programm works all fine without the loop (but receiving an error message, that during the simulation, Simulink window can't be closed).
I know there is a way to create a delay with waitforin matlab and create another function which I could call, but I couldn't figure out how to do this yet.
thanks
Regards
hohmchri
The right way to do this is to use the sim function to run your model (not the sequence of load_system, open_system and set_param that you have).
sim will block the execution of m-code until the model completes executing. Data can either be returned into the workspace (when used with no output arguments) or returned as an output from the call to sim. (And then you can write it to Excel as you've done.)
The only reason not to use sim, and perhaps use the commands you have, is if the model takes a long time to initialize, and you don't want to to open and close it every time through the loop. However, even in this case your code isn't correct. The load_system would be outside the loop; the open_system is not required; in your while loop you would poll the model's SimulationStatus property to see if it is still running (not the size of the y variable); and the close_system would be after the loop (as indicated by #m_power in one of the comments).
As written you should use the matlab pause command. This stops your execution for X seconds.
You should also look to optimize your code as m_power states

Matlab Simulink generating no output

I am learning Matlab Simulink. Here is the basic simulation that I'm doing.
When I pressed Start Simulation I get these errors.
Then as suggested here I changed my settings to this.
But now when I am simulating, a warning sound beeps but no error message is showing in Matlab Command Window and I am not getting my output.
Where is the problem and how can I solve it??
I am using Matlab 7.10.0(R2010a).
You're not getting any output in the Command Prompt because you haven't specified any sinks to export the data to the Workspace and so workspace variables aren't created. You're only able to see the output in the Scope tool in Simulink but this data hasn't been exported to your workspace. In Simulink, there is a To Workspace block that you can use to pipe the sinusoidal data to the workspace. In the Sinks section of the Library Browser, choose the To Workspace block and connect the output of the Sine Wave black to this block... so:
To make things compatible, make sure you choose the option to output your data as an array format instead of time series... well I find that the array option is better. Open up the To Workspace block and change the output to Array:
When you do this and run the simulation, you should see a variable called simout in your workspace that captures this data and tout that measures the time steps of each output amplitude in your sine data:
You can then plot the data by doing just plot(tout, simout);
Minor Note
The "warning" beep happens because MATLAB is telling you that Simulink has finished the simulation. Nothing wrong is happening.

How do I input variables into a Simulink model through MATLAB script (SimDriveline)

For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.

control simulink from M-file

I am trying to control a simulink from a M-file.
What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.
I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?
Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.
I stuck in this problem for a very long time, could someone help me with this please?
Thank you very much.
You can get the current time of a running simulation with:
get_param('simulink_model_name', 'SimulationTime');
So for instance by checking this value from your M-file during simulation by using
timer(...)
you can detect when the simulation is at 0.6 seconds.
I used a combination of simulink and m-script to achieve a similar goal.
In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:
set_param(bdroot,'SimulationCommand','pause');
run('myscript.m'); %insert the script name
set_param(bdroot,'SimulationCommand','continue');
Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock).
The second inport is connected to constant block with a value of 0.6
On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.
The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.
Let me know if it worked.
This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?