How to run a Simulink simulation with a push button?

19 views (last 30 days)
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?

Answers (3)

Paulo Silva
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
Walter Roberson
Walter Roberson on 23 Jun 2011
... Or the output went to a different workspace than was examined ?
Paulo Silva
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.

Sign in to comment.


rewerr
rewerr on 23 Jun 2011
My simulation has three "To Workspace" blocks. When I run my simulation in Simulink it produces an array and two structures. They are present in the Workspace! When I run my simulation from GUI it produces nothing.
  2 Comments
Paulo Silva
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
rewerr on 23 Jun 2011
Paulo Silva, it now works! Thank you all!

Sign in to comment.


rewerr
rewerr on 23 Jun 2011
Yet another problem. Running simulation works just fine. I added other commands to my GUI genareted m-file. Every command I added in my GUI genareted m-file produces nothing. No array, no struct in the Workspace. It seems they are all sent to some other workspace or... I don't know where to. When I run those commands from Matlab command prompt I can see a new array or struct in the Workspace. How to force GUI to only use standard Workspace?
  2 Comments
Paulo Silva
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
rewerr
rewerr on 23 Jun 2011
Paulo Silva, thanks again! Now I get it. You're very helpful!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!