pass a variable value from simulink to gui while running simulation

I have a simulink model which generates the position of a link using simmechanics. I need to use this value for the gui graph, and live. Is there a solution to this problem, since simout is only post-execution?

 Réponse acceptée

Orion
Orion le 19 Nov 2014
you can send your signal in an interpreted matlab function (or a S-function).
In this function (an external .m that you wrote), the code is executed at every step time during the simulation.
so you just have to get the handle of your gui components (with findall) and update your button / graphs wth the input signals.

6 commentaires

Thank you Orion
function y = fcn(u,v)
coder.extrinsic('assignin');
%#codegen
assignin('base','u',u);
assignin('base','v',v);
y = u;
This works well and the variables seem to get updated in workspace. Now I would like to have a live plot in the gui to show the values of (u,v). Any suggestions
Orion
Orion le 19 Nov 2014
Modifié(e) : Orion le 19 Nov 2014
Don't use the "Matlab function" block, use "Interpreted Matlab function".
See the elements I posted here
You're gonna need to do something like :
function y = MyExternalFcn(MyInputs)
% get the handle of your GUI and the handles structure
MyGui = findall(0,'Tag','MyGuiTag');
handles = guihandles(MyGui);
% here some code where you get the handles of you're different component, let say some edit box.
set(handles.editU,'String',num2str(MyInputs(1));
set(handles.editV,'String',num2str(MyInputs(2));
I'm pretty new to this, so couple of potentially silly questions 1. does MyGui refer to the name of the GUI file or just a variable 2. While using the below code to set the values in GUI: I've got a plot called axes1 in my GUI. Could you point me in the right direction on this please?
set(handles.editV,'String',num2str(MyInputs(2));
1. MyGui is neither the name, neither the tag of the GUI. this is the handle of the GUI.
2. Here, I suppose you made your GUI with guide.
By defaut, GUIDE tags your gui figure1 (see the value of tag in the inspector).
you need to retrieve the handle of your gui. one way is using the tag property (make sure the name is unique, don't tag 2 figures with the same string).
Now
% get the handle of your GUI
MyGuiHandle = findall(0,'Tag','figure1');
% get the structure of handles with has been stocked in it by GUIDE :
handles = guihandles(MyGui);
Then
% get the handle of the axe you want to modify (tagged _axes1_)
MyAxeHandle = handles.axes1;
And do whatever ou want with it.
axes(MyAxeHandle); % to make it the current axe
hold on;
plot(MyInputs(1),MyInputs(2),'b+');
Got 1). Used the following code:
function y = MyExternalFcn(MyInput)
MyGuiHandle = findall(0,'Tag','figure1');
handles=guihandles(MyGuiHandle);
MyAxeHandle = handles.axes1;
hold on;
plot(MyInput(1),MyInput(2),'b+');
This does capture my GUI but there seems to be no output displayed. Also Simulink execution time is set to 'inf' but execution stops after giving the error: 'Error using test (line 10)'; line(10) is plot... . Am I missing something here?
I have no idea what your data are.
To debug your problem, just remember that you are using a Mfile.
So you can put a breakpoint at the line 10 (and others if you want), then start the simulation, and every time your Mfile is called, the simulation will pause and you'll be inside the function.
And then, this no longer Simulink, but classic Matlab debugging.

Connectez-vous pour commenter.

Plus de réponses (1)

function y = MyExternalFcn(MyInput)
% get the handle of your GUI and the handles structure
% get the handle of your GUI
% global MyGuiHandle MyAxesHandle
MyGuiHandle = findall(0,'Tag','figure100'); %Tag of Figure
% assignin('base','MyGuiHandle',MyGuiHandle);
handles=guihandles(MyGuiHandle);
MyAxesHandle = handles.axes1;
axes(MyAxesHandle)
plot(MyAxesHandle,MyInput(1),MyInput(2),'bo');
y = 1;

Catégories

En savoir plus sur Simulink dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by