how to acess data from a simulink model and display it in GUIDE based GUI in real time
2 views (last 30 days)
Show older comments
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});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gui2 is made visible.
function gui2_OpeningFcn(hObject, eventdata, handles, varargin)
global var;
var.i1=1;
% opening simulink model which consists vrsink and executing it for a period of 110.7s
modelName = 'model_1';
open(modelName);
set_param(modelName,'SimulationCommand', 'start','StopTime', '110.7');
w = vrworld('vrml_1.wrl');
open(w);
view(w, '-internal');
vrdrawnow;
close(w);
%timer constructor
var.tmr=timer('TimerFcn',{@TimerCallback,hObject,handles},'Period',1,'ExecutionMode','FixedRate');
start(var.tmr);
% 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
var.i1=var.i1+1;
%set(handles.edit8,'String',var.x(var.i1,1));
% dispaly the value of the variable in an edit text box
set(handles.edit8,'String',var.x1);
%plot the value of the variable
plot(handles.axes1,'YDataSource','var.x1');
%hold .. to retain the previously plotted data
hold;
% to refresh plot as soon as variable changes
%refreshdata;
% to link the variable to the plot
linkdata on;
0 Comments
Answers (2)
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.
Jan Danek
on 4 Sep 2013
Hi Amit,
1) Have a look at the Simulink 3D Animation example:
vr_octavia_graphs
and its associated s-function:
octavia_graphs
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!
Jan
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!