Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulink-GUIDE - Unable to retrieve 'To Workspace' data and display on TextField on GUI

714 views
Skip to first unread message

st

unread,
Feb 20, 2011, 11:34:27 AM2/20/11
to
Hi all,

I am trying to display my value onwards from my 'To Workspace' block on the TEXT object on my GUI once after i pressed start button.

Below is the settings for my 'To Workspace' block :
- Variable name: simout
- Save format: Structure

My simulink model will simulate infinitely until the stop button is pressed. Once simulink model is started, model will keep on analyze the sound signal and display the result continuously on Display Block. At the same time, transfer the result to To Workspace block.

Below is the start button callback function:

function start_button_Callback(hObject, eventdata, handles)
% hObject handle to start_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set_param('GetFrequencyReal','SimulationCommand','start');
simdata = evalin('base', 'simout');
set(handles.note_string,'String',simdata);

I got some queries:

1) This is my error. Please advise i got such error.. I can't display the value from workspace to text object.

??? Error using ==> set
error: mxArray must be double, char, or cell.

Error in ==> mnrs>start_button_Callback at 156
set(handles.note_string,'String',simdata);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> mnrs at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback

2) Is it appropriate i put my code that display workspace value on text object under start button call back? Would it cause the program only take the workspace value at the instant of button is pushed? How should i do to make my program continuosly retrieve and display?

Many Thanks.

Donn Shull

unread,
Feb 20, 2011, 12:04:12 PM2/20/11
to
"st " <cel...@hotmail.com> wrote in message <ijrfqj$18l$1...@fred.mathworks.com>...

simdata is a structure. You will need to access the field of this structure that you want to disolay and then convert the value to a string to display it in your gui. Following your set command you will probably need to issue a drawnow command to get the display to update.

Hope this helps,

Donn

Phil Goddard

unread,
Feb 20, 2011, 12:52:27 PM2/20/11
to

> My simulink model will simulate infinitely until the stop button is pressed. Once
> simulink model is started, model will keep on analyze the sound signal and display
> the result continuously on Display Block. At the same time, transfer the result to
> To Workspace block.

To Workspace, and saving data to the workspace via a scope or display block, or saving data to the workspace in any of the other similar ways only save the data at the end of the simulation (or if it is paused).
They do not write to the workspace at every time step.
That would be way too slow.

Hence you're proposed approach to get data into your UI is fatally flawed.

You need to have something in the model that pushes the data to the UI.
This can be done using an event listener (http://www.mathworks.com/matlabcentral/fileexchange/24294-simulink-signal-viewing-using-event-listeners-and-a-matlab-ui is an example of this approach).
Or you could write an S-Function or a block callback to do something similar.

Phil.

st

unread,
Feb 21, 2011, 1:25:20 PM2/21/11
to

> You need to have something in the model that pushes the data to the UI.
> This can be done using an event listener (http://www.mathworks.com/matlabcentral/fileexchange/24294-simulink-signal-viewing-using-event-listeners-and-a-matlab-ui is an example of this approach).
> Or you could write an S-Function or a block callback to do something similar.
>
> Phil.

Hi Phil,

Thanks for your reply.

I have tried out the example that u posted. I noticed that it program the UI in .m file not .fig . But my current GUI is already developed and there is an AXES object in my GUI. If i have an AXES in simulink, how can i point the AXES in simulink as in the GUI's AXES.

I can't delete the GUI axes, as that AXES is also being used by other part of my program.

Thank you.

Phil Goddard

unread,
Feb 21, 2011, 7:49:07 PM2/21/11
to
Whether the UI is written completely from scratch or using GUIDE is irrelevant.
In both cases the code to attach, use and unattach the listener needs to be written by you.

GUIDE may generate a stub (sub)function where you may decide to put the listener code -- but that is in an m-file not the .fig file.

The axes issue is also irrelevant.
The example shows how to get data from the listener and put it into an axis on a UI.
That has nothing to so with Simulink (except that the data just happens to come from a listener attached to a Simulink model -- but the same procedure would be used irrespective of where the data comes from).

Phil.

st

unread,
Feb 27, 2011, 1:45:21 AM2/27/11
to
Hi Phil,

Thank you for your reply and the example. Your example is very helpful.

I have study the code but i am still that certain with the listener implementation.

Can I have your guide on how the listener works to retrieve the result from my simulink model to my GUI label during the simulation progress?

From the code i noticed that the starts button starts the function
localAddEventListener when the start button is being pushed.

The function will then trigger the localLoadModel then localEventListener will display the data on UI from simulink...

I tried to add the listener on my 6th Display block then call the listener to display the value on GUI label... but i failed to do it correctly due to lack understanding...

Can i have your advise on my code below? What i did wrongly???To avoid lengthy post, I only post the code of localAddLoadModel, localLoadModel, localEventListener that i have modified ... i have triggered it previously on my pushbutton function..

% Callback Function for adding an event listener to the gain block
function localAddEventListener

% get the application data
ad = guidata(gcbo);

% execute any original startFcn that the model may have had
if ~isempty(ad.originalStartFcn)
evalin('Base',ad.originalStartFcn);
end

% Add the listener(s)
% For this example all events call into the same function
ad.eventHandle = cell(1,length(ad.viewing));
for idx = 1:length(ad.viewing)
ad.eventHandle{idx} = ...
add_exec_event_listener(ad.viewing(idx).blockName,...
ad.viewing(idx).blockEvent, ad.viewing(idx).blockFcn);
end

% store the changed app data
guidata(gcbo,ad);

% Callback Function for executing the event listener on the gain block
function localEventListener(block, eventdata) %#ok

% get the application data
hf = findall(0,'tag',mfilename);
ad = guidata(hf);

% Get the handle to the line that currently needs updating
thisLineHandle = ...
ad.lineHandles([ad.viewing.blockHandle]==block.BlockHandle);

% Get the data currently on Display block displayed on label note_string
simdata = get(thisLineHandle,'Display');
set(handles.note_string,'String',num2str(simdata));

% Function to load model and get certain of its parameters
function ad = localLoadModel(modelName)

% List the blocks that are to have listeners applied
ad.viewing = struct(...
'blockName','',...
'blockHandle',[],...
'blockEvent','',...
'blockFcn',[]);
% Every block has a name
ad.viewing(1).blockName = sprintf('%s/Display',ad.modelName);

ad.viewing(1).blockHandle = get_param(ad.viewing(1).blockName,'Handle');

% List the block event to be listened for
ad.viewing(1).blockEvent = 'PostOutputs';

% List the function to be called
% (These must be subfunctions within this mfile).
ad.viewing(1).blockFcn = @localEventListener;


% Save some of the models original info that this UI may change
% (and needs to change back again when the simulation stops)
ad.originalStopTime = get_param(ad.modelName,'Stoptime');
ad.originalMode = get_param(ad.modelName,'SimulationMode');
ad.originalStartFcn = get_param(ad.modelName,'StartFcn');

% We'll also have a flag saying if the model has been previously built
ad.modelAlreadyBuilt = false;


and the error i got is

??? Reference to non-existent field 'modelName'.

Error in ==> mnrs>localLoadModel at 243
ad.viewing(1).blockName = sprintf('%s/Display',ad.modelName);

Error in ==> mnrs>mnrs_OpeningFcn at 79
ad = localLoadModel('GetFrequencyReal');

Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ==> mnrs at 42
gui_mainfcn(gui_State, varargin{:});


for now, i m still trying to understand the concept of listener... but please help me tru to debug my error... i find hard to get a very basic tutorial to understand the concept...

Many thanks...

Phil Goddard

unread,
Feb 27, 2011, 9:20:18 PM2/27/11
to

> and the error i got is
>
> ??? Reference to non-existent field 'modelName'.
>
> Error in ==> mnrs>localLoadModel at 243
> ad.viewing(1).blockName = sprintf('%s/Display',ad.modelName);
>

When I create the UI (in localCreateUI) I have the line
ad.modelName = modelName;

thus creating a field called modelName in the application data.
It seems that you have not copied that in your code.

Phil.

st

unread,
Feb 28, 2011, 1:18:04 AM2/28/11
to
Hii Mr.Phil,

Thank you for your reply.
I have put in the ad.modelName = modelName; in the localLoadModel function.

But i noticed that

1) If i run my GUI without open the Simulink Model file, i got error

---------------------------------------------------------------------------------------------------------------
??? Error using ==> mnrs>localLoadModel at 252
Invalid Simulink object name: GetFrequencyReal/Display.

Error in ==> mnrs>start_button_Callback at 159
localLoadModel(modelName);

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

Error in ==> mnrs at 42
gui_mainfcn(gui_State, varargin{:});

Error in ==> @(hObject,eventdata)mnrs('start_button_Callback',hObject,eventdata,guidata(hObject))


??? Error while evaluating uicontrol Callback

---------------------------------------------------------------------------------------------------------------


i already check my named my display block is Display... Why the error state cannot find my Display block...

2) If i run my GUI with Simulink block opened, no error on GUI compilation but error on the model block...

Error evaluating 'StartFcn' callback of block_diagram 'GetFrequencyReal'. Reference to non-existent field 'originalStartFcn'.

i already check i have the originalStartFcn in the .m file... but do i need to declare the originalStartFcn in the model??? I m very blur....

Please advise again... many thanks...

Other thing is, actually i m using the Display block to display my result from Embedded Matlab Function so that i can view the result... and now, i m wondering is it better and easier for me to pass the y value (from Embedded Block) to GUI and display it on Label? or how can i actually declare and variable that hold the y Value? I noticed that in your example, u r using scope and thus can get the simulink result via XData and YData... but i m quite blur how can i do that for Display block or Embedded function that output the Y value...


Many many many thanks..


please advise....

st

unread,
Feb 28, 2011, 3:08:08 AM2/28/11
to
Hi Mr.Phil,

I manage to solve the problem that load the simulink without open it by refering your example... I should include the modelLoaded = modelIsLoaded(modelName) function if i wish to start the simulink for GUI without open it .mdl file. Sorry of being careless.

But still, i wish to have your advise again for another problem. Here, i would like to shorten my message as my previous post quite lengthy. Sorry for that.

I got error after push my start button as below:


Reference to non-existent field 'originalStartFcn'.

Below is part of my code and your code that i tried to imitate:

--------------------------------------------------------------------------------------
function start_button_Callback(hObject, eventdata, handles)
modelName = 'GetFrequencyReal';
localLoadModel(modelName);
% set the stop time to inf
set_param(modelName,'StopTime','inf');
% set the simulation mode to normal
set_param(modelName,'SimulationMode','normal');
% Set a listener on the Gain block in the model's StartFcn
set_param(modelName,'StartFcn','localAddEventListener');
% start the model
set_param(modelName,'SimulationCommand','start');
% simdata = evalin('base', 'simout');
% set(handles.note_string,'String',simdata);
disp('Model is started');

--------------------------------------------------------------------------------------


function localAddEventListener
% get the application data
ad = guidata(gcbo);

% execute any original startFcn that the model may have had
if ~isempty(ad.originalStartFcn)
evalin('Base',ad.originalStartFcn);
end

% Add the listener(s)
% For this example all events call into the same function
ad.eventHandle = cell(1,length(ad.viewing));
for idx = 1:length(ad.viewing)
ad.eventHandle{idx} = ...
add_exec_event_listener(ad.viewing(idx).blockName,...
ad.viewing(idx).blockEvent, ad.viewing(idx).blockFcn);
end

% store the changed app data
guidata(gcbo,ad);

--------------------------------------------------------------------------------------


function localEventListener(block, eventdata) %#ok
% get the application data
hf = findall(0,'tag',mfilename);
ad = guidata(hf);

% Get the handle to the line that currently needs updating
thisLineHandle = ...
ad.lineHandles([ad.viewing.blockHandle]==block.BlockHandle);

% Get the data currently on Display block displayed on label note_string
simdata = get(thisLineHandle,'Display');
set(handles.note_string,'String',num2str(simdata));

--------------------------------------------------------------------------------------
function modelLoaded = modelIsLoaded(modelName)

try
modelLoaded = ...
~isempty(find_system('Type','block_diagram','Name',modelName));
catch ME %#ok
% Return false if the model can't be found
modelLoaded = false;
end

--------------------------------------------------------------------------------------
function ad = localLoadModel(modelName)

% Load the simulink model
if ~modelIsLoaded(modelName)
load_system(modelName);
end

% List the blocks that are to have listeners applied

ad.modelName = modelName;


ad.viewing = struct(...
'blockName','',...
'blockHandle',[],...
'blockEvent','',...
'blockFcn',[]);

ad.viewing(1).blockName = sprintf('%s/Display',ad.modelName);


ad.viewing(1).blockHandle = get_param(ad.viewing(1).blockName,'Handle');

ad.viewing(1).blockEvent = 'PostOutputs';
ad.viewing(1).blockFcn = @localEventListener;

ad.originalStopTime = get_param(ad.modelName,'Stoptime');
ad.originalMode = get_param(ad.modelName,'SimulationMode');
ad.originalStartFcn = get_param(ad.modelName,'StartFcn');

_-------------------------------------------------------------------------------------------------------

Actually i wonder... how can i specify the variable or parameters that represent the data from Display block... as in your example, you r retrieving your data with XData and YData in localEventListener function...
xdata = get(thisLineHandle,'XData');

Or can u teach me how can i add the listener to the Embedded Matlab Function Block so that i can display the Y value from that block to GUI's label instead of display block?
(Actually the Display block is connected to the Embedded Matlab Function Block so that i can view my result from that EMF block during simulation)

Thank you again Mr.Phil.

st

unread,
Feb 28, 2011, 4:30:56 AM2/28/11
to
Hi Mr.Phil and ppl,

I already tried to debug it... but i still got the error in the simulink block...

Reference to non-existent field 'originalStartFcn'.

I m not able to find out the bugssss...


Then i wonder... Do i need to define it or do some setting in the simulink .mdl file itself?
Why the error link always point to the simulink .mdl file?

In my .mdl file, i only include those usual DSP block as usual ... If i exclude the command for startFnc.. the Simulink file runs well just that the result from simulink is not transfered to UI...

Many thanks again..

st

unread,
Mar 2, 2011, 2:06:20 AM3/2/11
to
Hi Donn,

I tried to access the sturct (simout) but still unable to get it correct...can help me check out what is my problem?


-----------------------------------------------------------------
function start_button_Callback(hObject, eventdata, handles)

% Load the model if required (it may have been closed manually).
if ~modelIsLoaded('GetFrequencyReal')
load_system('GetFrequencyReal');
end


set_param('GetFrequencyReal','SimulationCommand','start');

disp('Model is started');

simdata = evalin('base', 'simout') ;

simdata = simdata.signals.values;
set(handles.note_string,'String',num2str(simdata));
------------------------------------------------------------------


Many thanks!

0 new messages