Matlab GUI Tutorial - Integrating Simulink Model into a GUI
30 Nov 2007 Quan Quach 20 comments 16049 views
How to specify other Simulink Parameters through the GUI
Specifying other Simulink Parameters through the GUI is not a difficult task. In this section of the tutorial, I will provide an example on how to do this. You can download the source files here if you do not want to read through the tutorial. Let’s say that we want to add the following capabilities to our GUI:
- set simulation time
- set sample time
- set the time when the unit step occurs
- designate the output parameter names
-
First, let’s make it so the user can specify the simulation time through the GUI. It is possible to specify the start and stop time as an argument to the
simcommand.For example, if I wanted to simulate the model for a specific period of time, I would use the following command:
sim('mass_spring',[0 simTime]);
If no time argument is given, then the value specified within the Simulink Model is used.
Another way of doing this is through the Simulink Model itself. To do this, we need to set the simulation time parameter to a variable. As can be seen in the image, I changed it to
simTime.
-
Next, double click on the “Step” block. Change the Step time field to
stepTimeand the Sample time field tosampleTimeas shown in the image below.
-
Finally, lets change the output block. Previously, the output was called yout by default. But suppose you want to give your output a more descriptive name, like displacement. Instead of using the Out1 sink, we would instead use the To Workspace sink. So go ahead and replace the old output sink with the new one.

-
Finally, we need to modify the properties of the output sink that we just added. Double click on the component and change the Variable name field to
displacementand change the Save format field toArray.
-
The Simulink model should now look like this:

-
Another thing you might want to do is to NOT limit the number of data points that result from your simulation. You can deactivate this by going to Simulation => Configuration Parameters from the Simulink Model.

-
Now, lets move on to the GUI changes. I won’t go over each step. But all we have to do is add on three Static Text Components and three Edit Text Components and to rename their Tag property appropriately. I renamed the Tag property for each of the Edit Text components to: stepTime_editText, sampleTime_editText, and simTime_editText. Here’s what your GUIDE figure should look like after you make the edits:

-
Finally, I made some minor edits to the simulate_pushbutton_Calback function.
function simulate_pushbutton_Callback(hObject, eventdata, handles) 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')); stepTime=str2num(get(handles.stepTime_editText,'String')); sampleTime=str2num(get(handles.sampleTime_editText,'String')); simTime=str2num(get(handles.simTime_editText,'String')); options = simset('SrcWorkspace','current'); sim('mass_spring',[],options); %plot the data plot(tout,displacement) xlabel('Time') ylabel('Displacement') title('2nd Order Mass Spring System') grid on
-
After you make all of these changes, you can finally test and run your GUI! Try inputting in different input parameters to test it out.

If you are unable to get your GUI/Simulink Model to work, you can download the source files here .
We encourage you explore and to play around with some of the other features within Simulink. There are a myriad of features that were not discussed, and we hope this tutorial has given you a better idea on how to use a GUI in conjunction with Simulink.
This is the end of the tutorial.
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