Thread Subject: GUI for RTW Simulink Model

Subject: GUI for RTW Simulink Model

From: Giacomo

Date: 6 Jul, 2009 13:28:01

Message: 1 of 6

I have realized a model with Simulink that inflate a container with air in steps. Everything works fine, now I'd like to realize a GUI where I can input the 3 paramenters needed to start the simulation and graph some results.
The problem I'm having is I'm not able to send the inputs from the GUI to the Simulink model....first because I used constant values for these parameters in the model (I edited them each simulation), second because the model must run in external mode with RTW. Can someone make me an example on how to change the input of the simulink model from a constant into the model to an input in the GUI?

For example: I need that the maximum pressure have to be 4 bar. Till now I simply put a constant block of with such value in the simulink model, now I'd like to exchange this with a GUI where I input 4 in a "edit text" and, when I click on the simulate button, simulink connect to the target and start the simulation.

Subject: GUI for RTW Simulink Model

From: Ralph Schleicher

Date: 7 Jul, 2009 21:15:12

Message: 2 of 6

"Giacomo " <bigbig1982@hotmail.com> writes:

> I have realized a model with Simulink that inflate a container with
> air in steps. Everything works fine, now I'd like to realize a GUI
> where I can input the 3 paramenters needed to start the simulation and
> graph some results. The problem I'm having is I'm not able to send
> the inputs from the GUI to the Simulink model....first because I used
> constant values for these parameters in the model (I edited them each
> simulation), second because the model must run in external mode with
> RTW. Can someone make me an example on how to change the input of the
> simulink model from a constant into the model to an input in the GUI?

Read about the Rsim target in the RTW manual. For offline graphics,
the "To MAT-file" block is sufficient. For online graphics, you need
a third-party data transfer tool between the Rsim executable and the
GUI. I've written such a tool in the past. It's quite simple.

--
Ralph Schleicher <http://ralph-schleicher.de>

Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools

Subject: GUI for RTW Simulink Model

From: Phil Goddard

Date: 8 Jul, 2009 00:20:04

Message: 3 of 6


You don't need RSIM target for either of the things you are wanting to do.

As part of a callback in your UI you need to do specify a new value for your constant block.
This can be done using
set_param('FullNameOfConstantBlock','Value','NewValueAsAString');

See the doc for set_param if you're unsure of how to use it.

This will work when the model is running in either simulation or External mode.

See
http://www.mathworks.com/matlabcentral/fileexchange/24294
for an example of doing the same thing but with a gain block rather than a constant block.

Phil.

Subject: GUI for RTW Simulink Model

From: Giacomo

Date: 9 Jul, 2009 07:56:02

Message: 4 of 6

I tried to use the set_param command, but I get another error:

??? Error using ==> Inflating_Test_Panel>start_pushbutton_Callback at 76
Cannot perform command line simulation of 'Inflating_Test_Model6' in external
mode.

Error in ==> gui_mainfcn at 96
        feval(varargin{:});

Error in ==> Inflating_Test_Panel at 16
    gui_mainfcn(gui_State, varargin{:});

Error in ==>
guidemfile>@(hObject,eventdata)Inflating_Test_Panel('start_pushbutton_Callback',hObject,eventdata,guidata(hObject))


??? Error while evaluating uicontrol Callback

Seems model cannot go on external mode, but in Simulink works perfectly.
This is the m.file of the GUI, I'm stunned :(

function varargout = Inflating_Test_Panel(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
                   'gui_Singleton', gui_Singleton, ...
                   'gui_OpeningFcn', @Inflating_Test_Panel_OpeningFcn, ...
                   'gui_OutputFcn', @Inflating_Test_Panel_OutputFcn, ...
                   'gui_LayoutFcn', [] , ...
                   'gui_Callback', []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function Inflating_Test_Panel_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

guidata(hObject, handles);


function varargout = Inflating_Test_Panel_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;

function input_mp_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
     set(hObject,'String','0')
end
guidata(hObject, handles);


function input_mp_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function start_pushbutton_Callback(hObject, eventdata, handles)
options = simset('SrcWorkspace','current');
sim('Inflating_Test_Model6',[],options);
set_param('Inflating_Test_Model6', 'SimulationMode', 'external');
set_param(gcs,'SimulationCommand','connect');
set_param(gcs,'SimulationCommand','start');
set_param('Inflating_Test_Model6','max_pressure',num2str(mp));

Subject: GUI for RTW Simulink Model

From: Phil Goddard

Date: 9 Jul, 2009 13:35:17

Message: 5 of 6


Where do you load the model?
Where do you start the code that external mode connects to?

Have a look at the start function in the example I pointed to earlier.

Phil.

Subject: GUI for RTW Simulink Model

From: Giacomo

Date: 9 Jul, 2009 15:04:02

Message: 6 of 6

"Phil Goddard" <philNOSPAM@goddardconsulting.ca> wrote in message <h34rml$f7u$1@fred.mathworks.com>...
>
> Where do you load the model?
> Where do you start the code that external mode connects to?
>
> Have a look at the start function in the example I pointed to earlier.
>
> Phil.

You were right Phil, strings were in wrong places, now it works thank you! :)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
parameter tuning Phil Goddard 7 Jul, 2009 20:24:04
ui Phil Goddard 7 Jul, 2009 20:24:04
external mode Phil Goddard 7 Jul, 2009 20:24:04
simulink Phil Goddard 7 Jul, 2009 20:24:04
external mode Giacomo 6 Jul, 2009 09:29:12
gui Giacomo 6 Jul, 2009 09:29:12
simulink Giacomo 6 Jul, 2009 09:29:12
rssFeed for this Thread

Contact us at files@mathworks.com