Code covered by the BSD License  

Highlights from
GUI development for SIMULINK models

GUI development for SIMULINK models

by

 

A tutorial on integrating GUI developed in GUIDE with SIMULINK model

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

opgui_sf(t,x,u,flag,Ts)
%Nitin skandan, 11-Aug-2011
% This is the s function which passes data from model to GUI
function [sys,x0,str,ts] = opgui_sf(t,x,u,flag,Ts)
%Control GUI for DRS

%   Nitin Skandan 27-1-2011
switch flag,
  case 0,
    [sys,x0,str,ts]=mdlInitializeSizes(Ts); 
  case 2,
    sys=mdlUpdate(t,x,u,Ts);
  case 3,
    sys = mdlOutputs(t,x,u); % Calculate outputs
  case { 1, 4, 9 },
    sys = [0 0 0 0];
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end
% end drs_switch

function [sys,x0,str,ts]=mdlInitializeSizes(Ts)

% call simsizes for a sizes structure, fill it in and convert to a sizes array.
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 1;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);
x0  = [0];
str = [];
ts  = [Ts 0];
% create the figure, if necessary
OpGUI;
% end mdlInitializeSizes

function sys=mdlUpdate(t,x,u,Ts)
fig = get_param(gcbh,'UserData'); %get figure handle
sys=x;
if ishandle(fig), % 
  if strcmp(get(fig,'Visible'),'on'),
      
      chnd = findobj(fig,'Tag','editbox'); % get handle to editbox
      str = floor(u(1)); 
      if num2str(str)
          set(chnd, 'String', str) ; %set value of editbox
      else
          set(chnd,'String','NA') ; %set value of editbox
      end

      x = [ 0 0.01 0.02 0.1 0.4 0.8 0.85]*u(2) ; %generate data to plot 
      y = [ 0 0.1 0.2 0.15 0.24 0.81 0.5]*u(2) ;
      
      chnd = findobj(fig,'Tag','plotx'); % find handle of plot object
      plot(chnd, x, y);        % plot    
      set(chnd, 'XLim', [0 5]) ;
      set(chnd, 'YLim', [0 5]) ;       
      set(chnd,'Tag','plotx') ; % Tag seems to be set to NULL after every plot, so this line is needed
  end
end
% end mdlUpdate

function sys = mdlOutputs(t,x,u)

 sys = [];
% end mdlOutputs

Contact us