Matlab GUI Tutorial - Integrating Simulink Model into a GUI
30 Nov 2007 Quan Quach 20 comments 16048 views
In this part of the tutorial, we are going to code the GUI so that it can call the Simulink Model.
The GUI portion Method 1: using simset
The simset command allows you to define which workspace to run your simulink model from. By default, the simulink model is run from is the main workspace.
-
Type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

-
The following is the code for the simulate_pushbutton_Callback functon.
axes(handles.axes1) %set the axes m=str2num(get(handles.mass_editText,'String')); %fetch the mass value c=str2num(get(handles.damping_editText,'String')); %fetch the damping value k=str2num(get(handles.spring_editText,'String')); %fetch the spring constant %configure the options so that the current %workspace is used in simulating the model options = simset('SrcWorkspace','current'); %the sim command simulates the simulink model %the first argument is the model name %the second argument is an array containing the start and stop time %if [] is used, then the value from within the simulink model is used %the third argument is the options configured from simset sim('mass_spring',[],options); %plot the data plot(tout,yout) xlabel('Time') ylabel('Displacement') Title('2nd Order Mass Spring System') grid on
-
Now, we are ready to run the GUI. You should see the following GUI appear. Try inputting different parameters and simulating the system response. Be sure the parameters are well defined (no letters, no symbols, no negative numbers, etc) or an error will result.

The GUI portion Method 2
This method is not as elegant as the previous method, but it is how I first learned how to run simulink models within GUIs.
-
Type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

-
Here is what the GUI should look like when you open it:

-
Double click on the “Simulate” Button to bring up the Property Inspector. Change the Callback property to
simulateButton. Now, each time the “Simulate” button is pressed, the callback is mapped to the simulateButton m-file instead of the normal callback.
-
The following is the code for simulateButton.m. This file is included in the zip folder you downloaded earlier. Notice that this m-file is NOT in function format. Instead, all of these commands are being executed in the main workspace area! Since all the parameters are defined in the main workspace, when we use the
simcommand to simulate the model, the variables m,c,and f are defined!! This would not have been possible if a normal callback was used.%notice that this m-file is NOT a function because simulink models %only allows you to use variables that are within the main workspace clear all %make the handles structures available to the main workspace h =gcf; handles = guidata(h); %set the axes to which the data will be plotted to axes(handles.axes1); % get the parameters from the edit text fields m=str2num(get(handles.mass_editText,'String')); c=str2num(get(handles.damping_editText,'String')); k=str2num(get(handles.spring_editText,'String')); %simulate the system sim('mass_spring'); %plot the data plot(tout,yout) xlabel('Time') ylabel('Displacement') Title('2nd Order Mass Spring System') grid on
-
Now, we are ready to run the GUI. You should see the following GUI appear. Try inputting different parameters and simulating the system response. Be sure the parameters are well defined (no letters, no symbols, no negative numbers, etc) or an error will result.

The next section will discuss how to configure other Simulink parameters such as the sources, sinks, time steps, simulation time, and some other things.
20 Responses to “Matlab GUI Tutorial - Integrating Simulink Model into a GUI”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

I am new to matlab, mainly I work on simulink. I am using Your GUI tutorial to learn GUIDE. All your tutorials are very good and very much explanatory.
In your .m script you used plot (tout, yout), but I don see these variable anywhere neither in the .m file nor in the model. And when I changed these variables name, they were still working fine. Suppose I have one more output port from Gain1 and want to plot (Gain1 Vs time) along with (Displacement Vs time) , want to show to plots in the GUI how should I proceed, shall I use plot(tout, yout) for(Disp. Vs time) and plot(tout, yout1) for (Gain1 Vs time).
It will be great of help if you can put tutorials on simulink GUI integration, like
1. How to integrate Source/Sink block into GUI,
2. How to give option like run, pause, and stop as in simulink in GUI.
3. How to specify step time as in simulink for a model in GUI.
4. How to integrate simulink menu bar some features with GUI like Configuration parameters.
5. How to see output in simulink scope in GUI etc…
Incorporate more of simulink related tutorial into your web w.r.t GUI, as there are very fewer amount of information available.
subin,
I will try to edit the tutorial to answer these questions. Thank you for the feedback!
You can use simset command to change the workspace option for the simulink model.
function runSimulinkModel(m,c,k)
options = simset(’SrcWorkspace’,'current’);
sim(’mass_spring’,[],options);
Chun-Chih,
Thanks for the tip!! That is extremely helpful. I have edited the tutorial to reflect this
Hello,
I need a little help in implementing simulink block via GUI. Ive followed every single steps you had put up, however the gui just doesnt work!!!rather depressed with it….i just couldnt make my simulink block work with gui…
Any tips??
Regards, Reha
Hi reha,
Take a look at the source files and see how it’s implemented, I’m not sure what your specific problem is, so it’s hard for me to help you.
Quan
Heya Quan,
Thanks for the offer. Ive solved the problems now.. Thanks for the tutorial. They were of much help. =D
Thanks,
Reha
Hi,
I am now implement the 555 Astable Simulink model circuit in the MATLAB GUI. After read through your tutorial, I manage to run the simulink model from the GUI. However my circuit model in the simulink is running infinitely, so may I know how can I stop the simulation from the GUI? Based on following code.
% — Executes on button press in stopsimulation_pushbutton.
function stopsimulation_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to stopsimulation_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set_param(handles.Astable_555_timerIC_voltage_measurement_edit, ‘SimulationCommand’, ’stop’)
set_param(’Astable_555_timerIC_voltage_measurement_edit’, ‘SimulationCommand’, ’stop’)
set(handles.text6, ‘String’, get_param(’Astable_555_timerIC_voltage_measurement_edit’, ‘SimulationStatus’))
set(handles.dynamic_plant_state, ‘String’, ’sim stopped’)
set_param(handles.modelname, ‘SimulationCommand’, ’stop’)
set(handles.dynamic_plant_state, ‘String’, ’sim stopped’)
set(handles.play,’value’,0);
set(handles.play,’enable’,'on’);
Besides that, how can I pop up the simulink model that running by GUI when click the “Simulate” button or another button that specially for this function? Based on following code.
% — Executes on button press in design_pushbutton.
function design_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to design_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Thank you for your help.
Hi, I am creating a GUI to interface my model. I want both display my simulation data and plot them. I use ‘to Workspace’ Simulink block to write simulation data on workspace but I am not able to access these data from the GUI and, as a consequence, to plot them in the GUI.
I have also tried to write simulation data in a .mat file but anyway I am not able to visualize these data. I would like to have my data shown in a sort of table in my GUI.
Can you help me?
Hello,
Excellent site and one I keep coming back to for new exercises to master.
I’ve had a look through the tutorials for an example that will return a simulink value to a box in the GUI.
Eg for a simplified value. I want to do 1+1 = 2. So in the GUI I have my two inputs which are sent to simulink to be added. The answer would then be dsiplayed in the GUI.
Is there an example like this so I can see how to pass the simulink answer back to the GUI. I know how to do it with plots so was thinking something similar should be quite easy.
Thanks and keep up the good work.
Hello,
Who would have thought the answer would be in the beginners tutorial for a simpler example.
I amalgamated this example with the simulink GUI example for a solution.
I used the simulatebutton option from the simulink-GUI tutorial and sent the data back to my model like that using the setup from the beginners tutorial. Not the tidiest but it works.
Thanks
I am doing a project on pc based monofilament winder using two stepper motors. And i want to simulate this using matlab simulink. So any body who is volentary to help me please share your idea. Thanks
Thanks for such a nice and easy tutorial thanks it is gonna help me a lot.
You can try using acslx.com software.
perfect !
hello !
I’m trying to implement in a gui a simulink scope, actually there are 4 scopes which i’d like to put on the UI, but I don’t know how.
Is there any possibility to run the finished gui without matlab installed , as a standalone application ??
Thanks alot, Keep UP !!
hello,
im trying to create an GUI where i take the output from the scope (saved in the workspace) to plot it in the GUI axes. the problem is that whenever i simulate the model from the GUI(using a push button) , the workspace variables are not getting updated.
can you help me with this??
Ashwin
Hi,
This is an excellent website and I’ve already learned so much! However, I have a question. Is it possible to change an embedded matlab function variable (in a simulink model) through a GUI?
I am modelling a vairable orifice and would like to change a variable in a a function while the simulation is running. Any help is greatly appreciated!
My model Simulink contains Stateflow blocks, when I start the programme GUI it shows an error:
??? Error using ==> sim
Error reported by S-function ’sf_sfun’ in ‘A_1/Ay (I,y)1/ SFunction ‘:
Stateflow Runtime Error (machine): Error evaluating machine workspace data ‘ks’ in MATLAB base workspace.
MATLAB expression ‘ks’ could not be evaluated.
ks variable is announced as ‘parameter’, if it is announced as ‘Constant’ model works, but ks variable value can be changed only in Simulink, and I need to change it in GUI.
Give me some advice.
Dear Sir,
Thanks for your release.That was realy helpfull. specialy the tutorials,they helped me alotl. Sir i have a little problem in connecting my simulink block to my GUI. In actual my requirement is to get data from my simulink in real time and plot it in real time on my GUI AXES. like on scope of simulink block. Please help me out.
Thanks in advance
Regards,
Bilal