Matlab GUI Tutorial - Integrating Simulink Model into a GUI
30 Nov 2007 Quan Quach 62 comments 1 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.
62 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
hello sir
i want to implement the block used in the paper”dynamic implementation of MEMS optical switches using simulink”.. am finding difficulty in implementing the block. kindly explain and help me.. its very urgent for my project..
Hi,
Love the tutorials, they have helped me immensely in my project. One problem i can’t solve though is sending the data in real time from simulink to the GUI. My simulink system has a video output that i want to view in a video window from within the GUI. If you have any ideas on this it would help me out immensely.
Thanks
hi….
thanks for your tutorials.
would you post tutorial about making GUI for TSP (Travelling Salesman Problem)?
hai..
I’m a new user of MATLAB..
can anyone help me to make a connection to other pc..
how to show webcam from the other pc in my pc?
I dont know what to do…
hello
I am from Colombia, and have a problem with the funntion sim for begin the simulink,
with the standalone aplication this show an error, ”undifined function sim for param type char”. Please need help.
Thank for you Colaboration.
Hi, great guide
Seeing that I havent work with simulink for a few years and I recently picked it up again, I wonder if you could help me out.
Im trying to alter some of the simulation blocks on my simulated system via a gui, and im talking about the hole block.
Imagine 3 generic blocks linked together with several predetermined inputs and outputs, what I wanted to do is change the system inside each of these blocks by selecting them from a list in my GUI.
Is this even possible?
Yes I beleive it is possible, although I haven’t worked with simulink in a long time as well! I’m sure it can be done though. Good luck, and sorry I could not be of more help
Hi,
I learned how to run simulink from GUI from your posting. I have few comments:
I can run the simulation using following code from gui
The above code does not allow simulation to be paused.
If I want to pause the simulation, I need to use set_param command as follows:
and then associate following code with PAUSE button
but the above code does not allow the option of ‘SrcWorkspace’ being set to ‘current’.
How should I go about it. I do not want the initialisation parameters to be made available to the MATLAb base workspace.
Thanks
hi,
i had a question, i wonder if it is possible to put a picture in the GUI page, to show a diagram or a process for example.
tnx
mitra
hi, would u like to explain on how to integrate simulink models that have base workspace and subsystems with GUI?
example, i wan to output the graph of one of the subsystems on the GUI.
hi, i have a simuink model that have more than ten subsystems.when i run the simulink alone, i have to import data call ‘ptp_diazepam’ to workspace to run the simulink.
I want to try output the graph of 1 of the subsystem which has the output “To workspace” block call Cvt_liver (the variable like in the case of your tutorial,i.e displacement)
i follow your code:
but i got this error:
Error using ==> lisaproj1(’intrabutton_Callback’,gcbo,[],guidata(gcbo))
Error using ==> sim
Unable to open model file ‘genpbpk.mdl’.
hope to hear from you soon! thanks a million
Hi
It was a great tutorial for GUI basics to link to Simulink.
Can you help in one thing though. I want to change my simulink block parameters when the simulation is going on. Is that possible?
If possible how i have to go about it……..
It would be of great help if you can explain this to me…
Thanks for the tutorial again
Ranjan
Hi, I don’t see the parameters ‘tout’ and ‘yout’ in plot(tout, yout); When i run it doesn’t show any graph in GUI, only show the FFT when i run the simulink; can u please help me with it? a little bit confused here. Thank you
Dear Quan,
How can I convert my .mdl file to standalone exe file
hi, this is samra i want matlab gui projects for my project presentation please help me.
Hi ,
I have finished my Matlab GUI and it works as desired. Is it possible make this GUI run as a stand alone application.
Im currently using Matlab 6.0 version. Can u guide me to make this a stand alone application or atleat give me a relavant link for me achieve this objective.
Thx
hi all
does anyone can tell me how i can run VR Sink Block in my simulink model by a pushbutton .
when i doubleclick VR Sink block in my simulink model it run simulation but i was unable to do the same thing using gui.
regards
What’s tout and yout, what’s the meaning of this command : plot(tout,yout)?
Is there any possibility to run the finished gui without matlab installed , as a standalone application ??
You will need MATLAB Compiler. Check out the command deploytool.
Hey,
I am making a GUI, that displays video using Simulink model. I have one problem: I need the video to be shown in the same window as buttons are, in the axes. I can’t find the solution. The video always opens in a separate window. Can anyone help me? I need it as fast as possible, thank you!
Here’s my code:
% — 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)
open_system(’dalis2′)
set_param(’dalis2/Gain’,'Gain’,'0′);
sim(’dalis2.mdl’);
% — Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
open_system(’dalis1′)
set_param(’dalis1/Gain’,'Gain’,'0′);
sim(’dalis1.mdl’);
% — Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%closes Simulink models and GUI
close_system(’dalis2′);
close_system(’dalis1′);
close(handles.figure1);
% — Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set_param(’dalis2/Gain’,'Gain’,'1′);
set_param(’dalis1/Gain’,'Gain’,'2′);
Hello,
thanks a lot for your guide tutorial. they have been really helpful and easy to understand.
i have succesfully, integrated my simulink model to GUI, but i would like to know-if possible- how to make plots in the GUI continuous(real time).
thanks for your help in advance.
Hi,
im urgently in need of a response to the last comment by me, please help!. thanks
sir,
i’ve created a simulink model and now i’d like to control it from a GUI. i’ve sucessfully been able to use the set_param command to control simulation.
the problem is that the output of my model is a video and as soon as the simulation starts, it appears in the M-Play video viewer automatically.
I want to embed the video output into the GUI instead. How do i do it??
Hello,
Your tutorials have been of a very great help to me!! kudos to u guys!!
I have a small problem…..I am trying to integrate the status of a checkbox in a GUI with that of a block parameter (for ex: my GUI contains a checkbox and depending on whether it is checked or not I would like to monitor the status of “LOG FIXED-POINT DATA AS A FI OBJECT” of SINK BLOCK PARAMETERS BLOCK) but I could not accomplish the task…hope you could help me out!!
Hello,
I’m trying to simulate my simulink model from the GUI I have created. But when I use the command “sim(’sfunctionxyz’)”, as you explain in the tutorial, the model doesn’t run and the command window print:
??? Simulink model ’sfunctionxyz’ was called with an invalid flag.
??? Error while evaluating uicontrol Callback.
do you have an idea of what matters with the command “sim”?
Hello,
I’m trying to simulate my simulink model from the GUI I have created. But when I use the command “sim(’sfunctionxyz’)”, as you explain in the tutorial, the model doesn’t run and the command window print:
??? Simulink model ’sfunctionxyz’ was called with an invalid flag.
??? Error while evaluating uicontrol Callback.
do you have an idea of what matters with the command “sim”?
I am working on a computer vision project, and I want to display a video sequence, which is the output from a simulink block, in an GUI axes, and I want it to be real-time. I’ve already tried the commands you explained, without succeding. Do you know whether there’s a different way to do want I want?
When I use the sim command, the command window prints: Memory allocation error, pointing to the line in which the sim command is used. Is there any possible solution?
Thanks in advance
yout is the output created in the massspring.mdl file
Hey,
thanks for the tutorial!
Is it possible to get a feedback from Simulink on how far the simulation has proceeded. e.g. implement a progressbar like the one used in simulink to show the simulations progress?
The progressbar itself doesn’t seem so much of a problem, but how do i get information back from simulink? Because the model time varies a lot depending on the selected input parameters, it’s not possible to ‘predict’ simulation time.
Any ideas?
Thanks!
Nevermind..just found your other tutorial and read through their comments
I have the same problem with my standalone aplication that show an error, ”undifined function sim for param type char”.
Could you help me, please?
Thank you.
I am finding problems in simulation for wind power quality.
Also how to apply wavelet toolbox for the same.
Olivia, if it says ‘undefined function’ that probably means you don’t have simulink installed or the proper toolbox (costs extra $$) from TheMathWorks. I find the same issue with lots of cool statistics functions that I want to use but can’t.
hi,i want your help
i make a project about image proceesing
and iuse simulink with Display block to show threshold value and use GUI with Edit text to show value of Display block in it but u can not do that
so how i take the the value of the display block from simulink to display in edit text which called ‘threshold’ in GUI and this value changed due to the image
thanks alot
please send the solution to my e-mail
Hello,
thanks for the tutorial ^_^
I’m having the problem that the simulink model takes ages to simulate if i call it from my GUI. If i start within simulink the simulation is finished after ~2minutes.
I cancelled the simulation from the GUI after 20minutes. Does anyone know how to solve this problem or can give me a reason for it so i might figure out a work-around?
Thanks for your help!
btw sorry to hear about you guys discontinuing the blog, thanks for all your hard work!
Hello,
Your tutorials are very helpful, thanks a lot! I’m wondering if you could help me with something. I’m trying to run an external executable in my MATLAB GUI. The executable is actually a simple fingerprint scanner GUI. I would like to somehow run this GUI inside the one I created in MATLAB. Is this possible … ? I haven’t been able to find much help on it. Sorry, this isn’t related to Simulink, but hope you are able to help anyway! Thank you in advance
P.S. I know I can use open() to run the executable inside MATLAB, but I specifically want it to run inside the GUI.
i need help regarding QAM modulation using simulink and parameers are controlled fromGUI………..
can anyone help me to do this task..
I have the same problem as Olivia, even trying with the tutorial example I get the following output when pushing the simulate button:
Zane Montgomery, if the application runs in Matlab, this means that simulink and the necessary toolbox are installed. The problem seems to be that we need to specify something else in the deploytool configuration.
Any ideas/ suggestions?
Sorry J.M. but I don’t have Simulink access to play around with it.
I recommend bringing your issue to the MathWorks forums where they get much higher traffic for questions: http://www.mathworks.com/matlabcentral/
good luck!
Thanks for the tutorial! Well done!!
Is there any way to deploy the GUI with the integrated model using the deployment tool. I think i have to change the sim and simset commands? Need some help, pls.
Hello and thank you very much for your great tutorials.
My question is that if there is a way to debug a GUI (and a simulink model), like the same way we debug an .m file? I’d like to see what goes on in my workspace.
Regards,
Faezeh