No BSD License  

Highlights from
Data Logging GUI Manager

image thumbnail
from Data Logging GUI Manager by John Stevens
GUI for easily modifying data logging parameters for Simulink models.

datalogmgr()
function datalogmgr()
% DATALOGMGR     Data Logging Manager.
%    DATALOGMGR() displays a dialog box which allows selection of a Simulink
%    model and displays all named signals and the corresponding data logging
%    parameters. These values can be edited in place and selecting OK will
%    update the Simulink model with the changes.
%
%    This function uses the createTable() function available from the
%    Matlab Central File Exchange (File ID: 14225).
%
%    Copyright 2008 John Stevens

%% Use system background color for GUI components
panelColor = get(0,'DefaultUicontrolBackgroundColor');

%% Get screen size
ss=get(0,'ScreenSize');
fraction = 0.6;
dlgpos = [ss(3)*(1-fraction)/2 ss(4)*(1-fraction)/2 ss(3)*fraction ss(4)*fraction];

%% ------------ Callback Functions ---------------

% Figure resize function
function figResize(src,evt)
    fpos = get(hfig,'Position');
    width = fpos(3);
    height = fpos(4);
    set(topPanel,'Position',[5.6 height-4.8 width-11.2 3.2])
    set(midPanel,'Position',[5.6 6.4 width-11.2 height-12.8])
    set(botPanel,'Position',[5.6 1.6 width-11.2 3.2])
end

% Top panel resize function
function topPanelResize(src,evt)
    tpos = get(topPanel,'Position');
    width = tpos(3);
    height = tpos(4);
    set(fnameLabel,'Position',[1 0.3 width-9.8 1.5]);
    set(getFileButton,'Position',[width-6.8 0.3 4.62 1.5]);
end

% Middle panel resize function
function midPanelResize(src,evt)
    mpos = get(topPanel,'Position');
end

% Bottom panel resize function
function botPanelResize(src,evt)
    bpos = get(topPanel,'Position');
    width = bpos(3);
    height = bpos(4);
    set(okButton,'Position',[width/2-13.5 0.5 10 2]);
    set(cancelButton,'Position',[width/2+3.5 0.5 10 2]);
end

%% Callback for getFileButton
function getFileButtonCallback(src,evt)
    [filename, pathname, filterindex] = uigetfile( ...
        {'*.mdl','Models (*.mdl)'; ...
        '*.*',  'All Files (*.*)'}, ...
        'Select a Simulink Model File');

    if filename==0
        error('Must select a valid model file.')
    end

    % Put filename in text box
    set(fnameLabel,'string',[pathname,filename]);
    
    % Load Simulink model
    load_system([pathname,filename])
    
    % Extract model name w/o .mdl extension
    sysname = strtok(filename,'.');
    
    % Get all named ports in model
    ports = find_system(sysname,'findall','on','followlinks','off','regexp','on','type','port','Name','.');

    % Get data from Simulink model
    dl_name        = get_param(ports,'name');
    dl_datalogging = get_param(ports,'datalogging');
    dl_dldecdata   = get_param(ports,'dataloggingdecimatedata');
    dl_decimation  = get_param(ports,'dataloggingdecimation');

    % Convert data from matrix to cell
    rows = size(dl_datalogging,1);
    cols = size(dl_datalogging,2);
    tmp1 = mat2cell(strcmp(dl_datalogging,'on'),ones(rows,1),ones(cols,1));
    tmp2 = mat2cell(strcmp(dl_dldecdata,'on'),ones(rows,1),ones(cols,1));
    
    % Put data into table
    set(mtable,'Data',[dl_name tmp1 tmp2 dl_decimation]);
    
end

%% Callback for okButton
function okButtonCallback(src,evt)
    % Get data from table
    data = cell(get(mtable,'Data'));
    
    % Convert data from cell to matrix
    dl_name = data(:,1);
    rows = size(dl_name,1);
    cols = size(dl_name,2);
    
    tmp1 = cell2mat(data(:,2));
    %assignin('base','tmp1',tmp1);
    rng_on = find(tmp1==1);
    rng_off = find(tmp1==0);
    dl_datalogging = cell(size(dl_name));
    dl_datalogging(rng_on) = {'on'};
    dl_datalogging(rng_off) = {'off'};
    %dl_datalogging = mat2cell(dl_datalogging,ones(rows,1),ones(cols,1));

    tmp2 = cell2mat(data(:,3));
    %assignin('base','tmp2',tmp2);
    rng_on = find(tmp2==1);
    rng_off = find(tmp2==0);
    dl_dldecdata = cell(size(dl_name));
    dl_dldecdata(rng_on) = {'on'};
    dl_dldecdata(rng_off) = {'off'};
    %dl_dldecdata = mat2cell(dl_dldecdata,ones(rows,1),ones(cols,1));

    dl_decimation = data(:,4);
    
    % Put data into Simulink model
    for i=1:length(ports)
        set_param(ports(i),'name',dl_name{i});
        set_param(ports(i),'datalogging',dl_datalogging{i});
        set_param(ports(i),'dataloggingdecimatedata',dl_dldecdata{i});
        set_param(ports(i),'dataloggingdecimation',dl_decimation{i});
    end
    
    % Save Simulink model
    save_system(sysname);
    
    % Close GUI
    close(hfig);
end

%% Callback for cancelButton
function cancelButtonCallback(src,evt)
    % Close Simulink model
    close_system(sysname);
    
    % Close GUI
    close(hfig);
end

%% ------------ GUI layout ---------------

%% Create figure
hfig = figure('Color',panelColor,...
    'Name','Data Logging Manager',...
    'HandleVisibility','callback',...
    'IntegerHandle','off',...
    'NumberTitle','off',...
    'Units','pixels',...
    'Position',dlgpos,...
    'ResizeFcn',@figResize);

set(hfig,'Units','characters')

%% Create a panel to hold model file selction control
topPanel = uipanel('Parent',hfig,...
	'Title','Simulink Model Filename',...
	'BackgroundColor',panelColor,...
    'Units','characters',...
	'Position',[5.6 26.8 100.8 3.2],...
	'BorderWidth',1,...
    'ResizeFcn',@topPanelResize);
          
%% Create a panel to hold the table data
midPanel = uipanel('Parent',hfig,...
	'BackgroundColor',panelColor,...
    'Units','characters',...
	'Position',[5.6 6.3 100.8 18.9],...
	'BorderWidth',1,...
    'ResizeFcn',@midPanelResize);

%% Create a panel to hold the button controls
botPanel = uipanel('Parent',hfig,...
	'BackgroundColor',panelColor,...
    'Units','characters',...
	'Position',[5.6 1.6 100.8 3.2],...
	'BorderWidth',1,...
    'ResizeFcn',@botPanelResize);

% topPanelResize(topPanel,[]);
% midPanelResize(midPanel,[]);
% botPanelResize(botPanel,[]);

pathname = '';
filename = '';
sysname = '';
ports = '';

%% Create Model File Text Box
fnameLabel = uicontrol('parent',topPanel,'style','text',...
	'HorizontalAlignment','left',...
	'BackgroundColor','white',...
	'units','characters',...
    'position',[1 0.3 90 1.5],...
	'string',[pathname,filename]);

%% Create Get File Button
getFileButton = uicontrol('parent',topPanel,'style','pushbutton',...
	'HorizontalAlignment','left',...
	'units','characters',...
	'position',[94 0.3 4.62 1.5],...
	'string','...',...
    'Callback',@getFileButtonCallback);

%% Create OK Button
okButton = uicontrol('parent',botPanel,'style','pushbutton',...
	'units','characters',...
	'position',[33 0.5 10 2],...
	'string','OK',...
    'Callback',@okButtonCallback);

%% Create Cancel Button
cancelButton = uicontrol('parent',botPanel,'style','pushbutton',...
	'units','characters',...
	'position',[50 0.5 10 2],...
	'string','Cancel',...
    'Callback',@cancelButtonCallback);

%% Create Data Table

% Make sure panel is created before making table
pause(0.1)

% Define table
mtable = createTable(midPanel,...
	{'Signal Name','Logged','Decimate','Decimation Value'},...
    [{'null'} {false} {false} {'null'}],...
    false,...
	'AutoResizeMode',...
	javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);

% Get table object
jtable = mtable.getTable;

% Change column widths
jtable.getColumnModel.getColumn(1).setMaxWidth(80);
jtable.getColumnModel.getColumn(2).setMaxWidth(80);

end

Contact us at files@mathworks.com