how to acess data from a simulink model and display it in GUIDE based GUI in real time

10 views (last 30 days)
Amit on 3 Sep 2013
Dear all,
I am working on a program, where I need to call a simulink function model at the start of the GUI. The simulink model in turn has a vrsink which calls a .wrl file. As soon as simulation starts, some variables are generated which guides the motion of object in vrml file. I want to access those variables in the GUI, so that we can clearly see them in a GUIDE generated GUI. So far my efforts were in vain. Its giving error that unknown variable, may be because the variable is getting generated after simulation starts, but as you can see in the code that simulation has been called before calling the variable. So I don't know whats the problem. Besides I also tried to declare a dummy variable so that I start getting the correct value after first update but the text box always showed the value of dummy variable itself. I tried using Linkdata on and refresh all both.
Any help will be appreciated.
% the purpose of the program is to run a simulink model (which contains a vrsink) as soon as the GUI opens. GUI has several display windows like edit text boxes and plots
% whose job is to display the real time parameters of the animated body.
function varargout = gui2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui2_OpeningFcn, ...
'gui_OutputFcn', @gui2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
gui_mainfcn(gui_State, varargin{:});
% End initialization code - DO NOT EDIT
% --- Executes just before gui2 is made visible.
function gui2_OpeningFcn(hObject, eventdata, handles, varargin)
global var;
% opening simulink model which consists vrsink and executing it for a period of 110.7s
modelName = 'model_1';
set_param(modelName,'SimulationCommand', 'start','StopTime', '110.7');
w = vrworld('vrml_1.wrl');
view(w, '-internal');
%timer constructor
% Choose default command line output for gui2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
function TimerCallback(obj,event,hObject,handles)
global var;
% accessing variable ver_d from workspace (ver_d is a one dimensional array)
var.x = evalin('base','ver_d');
% accessing an element of the array
var.x1 = var.x(var.i1);
% incrementing the counter to access the next element of the array
% dispaly the value of the variable in an edit text box
%plot the value of the variable
%hold .. to retain the previously plotted data
% to refresh plot as soon as variable changes
% to link the variable to the plot
linkdata on;

Answers (2)

Kaustubha Govind
Kaustubha Govind on 3 Sep 2013
Edited: Kaustubha Govind on 3 Sep 2013
I don't know anything about the 'VR Sink' block or about Simulink 3D Animation, but it seems plausible that the .wrl file that it writes to is not available until after simulation is complete. So, if you replace set_param(modelName,'SimulationCommand', 'start','StopTime', '110.7') with the SIM command (which returns only after the simulation is complete), you might have better luck. However, this may not be what you want, since it looks like you want to read the values simultaneously as the simulation runs.
I see that the VR Sink block has an option "Show video output port" that probably outputs the same data from a port on the block. Is this the data that you're trying to read? If so, I would recommend enabling that option, and then accessing the output signal of that block as described here.
  1 Comment
Amit on 5 Sep 2013
yes, you are right.. SIM is not a solution for me. Besides VR Sink block option "Show video output port", serves a different purpose, it pops up a the animation video in a small window so that we can view it. Thanks any way.

Sign in to comment.

Jan Danek
Jan Danek on 4 Sep 2013
Hi Amit,
1) Have a look at the Simulink 3D Animation example:
and its associated s-function:
Although this example shows the "reverse" approach - a GUI created upon starting the simulation in Simulink, it should serve as an illustration of data exchange you have in mind.
2) Another approach would be to use the Simulink 3D Animation MATLAB interface (not a Simulink signal connection) to get data from the Virtual Reality world during simulation. You can do this by opening the same virtual world that is open in Simulink by VR Sink also by a GUI callback, in which you use vrnode/getfield to read particular object property. In this case, it is up to you to arrange timing - it is perhaps better to use the former approach.
Good luck!
  1 Comment
Amit on 5 Sep 2013
1) vr_octavia_graphs is not present in my version of the matlab, anyways..I will try to find whether any of my friends have that.
2) yes, you are right I have not tried the other approach to access the parameters, I will try and let you know the results. Thanks.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!