Matlab GUI Tutorial - Integrating Simulink Model into a GUI
30 Nov 2007 Quan Quach 26 comments 19684 views
The Simulink Model
In this part of the tutorial, we will quickly go over the Simulink Model. The Simulink Model is of a mass-spring-damper system, which is a simple 2nd order system. The following is a free body diagram of the mass-spring-damper system.

It is assumed here that you know how to use Simulink and understand the basic theory of the mass-spring-damper system, so I will not go into any detail on how the model was generated or how equations were derived. If you are clueless on using Simulink, this is a good place to visit.
-
First, download the sample files provided here. Unzip the files and place them wherever you please. The zip file includes 4 files: simulink_gui.fig, simulink_gui.m, simulateButton.m, and mass_spring.mdl.
-
The following is a diagram of the Simulink Model for the above system.

-
Notice that the Gain blocks use variables instead of number values: 1/m, c/m, and k/m. To simulate this system from the command line, we use the
simcommand. Since the Simulink Model is linked to the main workspace by default, the parameters m,c and k must be defined within the main workspace in order for thesimcommand to work properly. Otherwise, an error will result. There are ways to get around this using the commandsimset, and we will discuss this in the next section. -
Thus, if the variables m,c, and k are not defined in the main workspace and we try to use a function like the one listed below, an error would result!
function runSimulinkModel(m,c,k) sim('mass_spring');
-
This can be especially troublesome if you are trying to interface your Simulink Model into a GUI because GUIs store all their information outside from the main workspace. In the next section, we will discuss two ways to resolve this problem.
26 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?