How to run a Simulink simulation with a push button?
19 views (last 30 days)
Show older comments
I'm a beginner. My simulation works OK in Simulink. It also works from Matlab command prompt. I can't run it from GUI. I'd like to start my simulation with a push button. I edited some code in GUI genareted m-file:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sim('MySimulation.mdl');
My simulation doesn't start. I don't see any array or structure in Matlab workspace. Why?
0 Comments
Answers (3)
Paulo Silva
on 23 Jun 2011
%load or open the model if it isn't already
open_system('MySimulation.mdl') %load_system('MySimulation.mdl')
set_param('MySimulation.mdl', 'SimulationCommand', 'start')
You don't see any exported variable because it didn't simulate or you just forgot to use To Workspace blocks or to select some variables to export on the simulation configuration.
2 Comments
Paulo Silva
on 23 Jun 2011
Walter I woudn't go that far, the default behavior is to send to MATLAB workspace unless someone changes it on purpose.
rewerr
on 23 Jun 2011
2 Comments
Paulo Silva
on 23 Jun 2011
Ok Walter might be right, do this and try again
opts = simset('DstWorkspace','base')
sim('MySimulation.mdl',[], opts);
rewerr
on 23 Jun 2011
2 Comments
Paulo Silva
on 23 Jun 2011
So the problem was solved but returned, you give no details how you are doing the code, the things you are expecting to go to the MATLAB workspace are really going to the GUI workspace, you can see it by doing the function who after the simulation (who goes inside you GUI code right after the sim or set_param), let me guess some workarounds:
[t,x,y] = sim(model,timespan,options);
assignin('base','t',t); %save the t to MATLAB workspace
assignin('base','y',y);
or if you use the set_param to start it do the assignin the next lines, so the variables exported by the simulation can be saved into your MATLAB workspace
See Also
Categories
Find more on Model, Block, and Port Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!