MATLAB: How to use a timer to update real time data from Simulink

guisimulinktimer

Hi Everyone!
I am creating a program that reads real-time data from Simulink and shows it in the GUI. The thing is that I want it to read the data from each value (in displays) every seconds, and I can't find the way.
I have made it read the data every time I click on read…but I want it to be done automatically…
Thanks in advance
THE CODE:

% % Un programa creat per Marc Céspedes Esteve % %
% % El programa et deixa triar el model i el carrega, de manera que carrega els valors estàndards i els resultats (displays) % %
% % Deixa variar els valors estàndards % %
% % Guarda els resultats? % %
% % Deixa triar nom per un excel? % %
% % Guarda dades? % %
function varargout = Test(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Test_OpeningFcn, ...
'gui_OutputFcn', @Test_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 Test is made visible.
function Test_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for Test
handles.output = hObject;
%Set timer
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@ActualitzaVariables,hObject}); % Specify callback
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = Test_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
function edit_simfile_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.

function edit_simfile_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Controla el botó per escollir l'arxiu
function pushbutton_simbrowse_Callback(hObject, eventdata, handles)
[input_file,pathname] = uigetfile( ...
{'*mdl','mdl Files (*.mdl)';...
'*.*', 'All Files (*.*)'}, ...
'Select files', ...
'MultiSelect', 'on');
if pathname == 0
return
end
% Agafa el nom dels arxius escollits
inputfile= fullfile(pathname,input_file);
current_folder=strcat(cd,'\');
mdlname=strrep(inputfile,current_folder,'');
mdlname=strrep(mdlname,'.mdl','');
% Mostra el nom del model a la GUI
set(handles.edit_simfile,'String',mdlname);
guidata(hObject, handles);
% Borra el programa carregat
function pushbutton_simremove_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
set(handles.edit_simfile,'String','');
guidata(hObject, handles);
% Carrega el model i els seus paràmetres
function pushbutton_loadmodel_Callback(hObject, eventdata, handles)
modelname=get(handles.edit_simfile,'string');
if isempty(modelname)
errordlg('Cap model escollit!');
end
checkload=~isempty(find_system('type','block_diagram','name',modelname));
if checkload==0
try
load_system(modelname);
catch
end
end
open_system(modelname);
block_Std_PermeatePressure = sprintf('%s/Std_PermeatePressure' ,modelname);
block_Std_MembraneFeedPressure = sprintf('%s/Std_MembraneFeedPressure' ,modelname);
block_Std_PressureDrop = sprintf('%s/Std_PressureDrop' ,modelname);
block_Std_MembraneFeedOsmoticPressure = sprintf('%s/Std_MembraneFeedOsmoticPressure' ,modelname);
block_Std_TCF = sprintf('%s/Std_TCF' ,modelname);
block_Std_FeedConcentrateConcentration = sprintf('%s/Std_FeedConcentrateConcentration' ,modelname);
block_Std_PermeateOsmoticPressure = sprintf('%s/Std_PermeateOsmoticPressure' ,modelname);
% %Display

% block_BetaB = sprintf('%s/BetaB' ,modelname);
%Carrega valors del model inicials i els guarda en variables
PermeatePressureValue = get_param(block_Std_PermeatePressure, 'Value');
MembraneFeedPressureValue= get_param(block_Std_MembraneFeedPressure, 'Value');
PressureDropValue= get_param(block_Std_PressureDrop, 'Value');
MembraneFeedOsmoticPressureValue = get_param(block_Std_MembraneFeedOsmoticPressure, 'Value');
TCFValue = get_param(block_Std_TCF, 'Value');
FeedConcentrateConcentrationValue = get_param(block_Std_FeedConcentrateConcentration, 'Value');
PermeateOsmoticPressureValue = get_param(block_Std_PermeateOsmoticPressure, 'Value');
% %Display
% BetaValue = get_param(block_BetaB, 'Outport');
% Carrega valors a GUI
set(handles.PermeatePressure, 'string', num2str(PermeatePressureValue));
set(handles.MembraneFeedPressure, 'string', num2str(MembraneFeedPressureValue));
set(handles.PressureDrop, 'string', num2str(PressureDropValue));
set(handles.MembraneFeedOsmoticPressure, 'string', num2str(MembraneFeedOsmoticPressureValue));
set(handles.TCF, 'string', num2str(TCFValue));
set(handles.FeedConcentrateConcentration, 'string', num2str(FeedConcentrateConcentrationValue));
set(handles.PermeateOsmoticPressure, 'string', num2str(PermeateOsmoticPressureValue));
% %Display
% set(handles.Display10, 'string', num2str(BetaValue));
% Per tal que puguin ser accedits per les callbacks
handles.modelname=modelname;
handles.block_Std_PermeatePressure = block_Std_PermeatePressure;
handles.block_Std_MembraneFeedPressure = block_Std_MembraneFeedPressure;
handles.block_Std_PressureDrop = block_Std_PressureDrop;
handles.block_Std_MembraneFeedOsmoticPressure = block_Std_MembraneFeedOsmoticPressure;
handles.block_Std_TCF = block_Std_TCF;
handles.block_Std_FeedConcentrateConcentration = block_Std_FeedConcentrateConcentration;
handles.block_Std_PermeateOsmoticPressure = block_Std_PermeateOsmoticPressure;
% %Display
% handles.block_BetaB = block_BetaB;
guidata(hObject,handles);
% Per controlar el valor de Standard Permeate Pressure
function PermeatePressure_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.PermeatePressure,'value',str2double(val));
set_param(handles.block_Std_PermeatePressure,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function PermeatePressure_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Membreane Feed Pressure
function MembraneFeedPressure_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.MembraneFeedPressure,'value',str2double(val)); %No cal crec

set_param(handles.block_Std_MembraneFeedPressure,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function MembraneFeedPressure_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Pressure Drop (change)
function PressureDrop_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.PressureDrop,'value',str2double(val)); %No cal crec
set_param(handles.block_Std_PressureDrop,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function PressureDrop_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Membrane Feed Osmotic Pressure
function MembraneFeedOsmoticPressure_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.MembraneFeedOsmoticPressure,'value',str2double(val)); %No cal crec
set_param(handles.block_Std_MembraneFeedOsmoticPressure,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function MembraneFeedOsmoticPressure_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Temperature Correction Factor
function TCF_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.TCF,'value',str2double(val)); %No cal crec
set_param(handles.block_Std_TCF,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function TCF_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Feed Concentrate Concentration
function FeedConcentrateConcentration_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.FeedConcentrateConcentration,'value',str2double(val)); %No cal crec
set_param(handles.block_Std_FeedConcentrateConcentration,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function FeedConcentrateConcentration_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Per controlar el valor de Standard Permeate Osmotic Pressure
function PermeateOsmoticPressure_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
val =get(hObject,'string');
set(handles.PermeateOsmoticPressure,'value',str2double(val)); %No cal crec
set_param(handles.block_Std_PermeateOsmoticPressure,'Value',num2str(val));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function PermeateOsmoticPressure_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Botó d'iniciar el model o parar-lo quan estigui encès.
function pushbutton_run_Callback(hObject, eventdata, handles)
flag=get(hObject,'string');
if strcmp(flag,'RUN')==1
set_param(handles.modelname,'SimulationCommand','Start');
set(hObject,'string','STOP');
if strcmp(get(handles.timer, 'Running'), 'off')
start(handles.timer);
end
else
set_param(handles.modelname,'SimulationCommand','Stop');
set(hObject,'string','RUN');
if strcmp(get(handles.timer, 'Running'), 'on')
stop(handles.timer);
end
% Destroy timer
delete(handles.timer)
%delete(hObject);
end
guidata(hObject,handles);
% Mostrar dades displays
function Display10_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function Display10_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton_read.
function pushbutton_read_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
%Beta

rto10 =get_param('CPASimulinkTempsRealNoms/BetaB', 'RuntimeObject');
BetaValue = rto10.InputPort(1).Data;
set(handles.Display10, 'string', num2str(BetaValue));
%Membrane Feed Concentration

rto1 =get_param('CPASimulinkTempsRealNoms/MembraneFeedConcentration', 'RuntimeObject');
MembraneFeedConcentrationValue = rto1.InputPort(1).Data;
set(handles.Display1, 'string', num2str(MembraneFeedConcentrationValue));
%System Feed Flow

rto2 =get_param('CPASimulinkTempsRealNoms/SystemFeedFlow', 'RuntimeObject');
SystemFee

Best Answer

Marc - presumably it is the code in your pushbutton_read_Callback that you wish to call periodically. Try to do the following - in the _OpeningFcn of your GUI, you could create and start a timer as
% Choose default command line output for ReadFromWorkspace
handles.output = hObject;
% create the timer object
handles.timer = timer('Name','ReadFromSimulink', ...
'Period',1, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
% Update handles structure
guidata(hObject, handles);
% start the timer
start(handles.timer);
In the above we create and start a timer that will fire every one second (period). The callback that fires is timerCallback and it takes one additional input parameter which is the handle to the GUI figure. Now we define the callback (in the GUI) as
function timerCallback(~,~,hFigure)
% get the handles to the controls
handles = guidata(hFigure);
% add the code from your pushbutton function
In the above, we use the guidata function to get the handles structure so that we can update any controls on the GUI with the data that we have read in from Simulink. Note that you may want to add some code to the DeleteFcn of your GUI to stop the running timer. Something like
function yourGuiName_DeleteFcn(hObject, ~, handles)
try
stop(handles.timer);
delete(handles.timer);
handles.timer = [];
guidata(hObject,handles);
catch
% intentionally left blank
end
Try the above and see what happens!
Related Question