No BSD License  

Highlights from
Unit Conversion Tools

image thumbnail

Unit Conversion Tools

by

 

04 Sep 2007 (Updated )

Command line, GUI, & Simulink block to convert physical units

lib_unit_conversion_mask(cmd)
function lib_unit_conversion_mask(cmd)
%Callbacks & setup functions for the lib_unit_conversion.mdl mask
% -------------------------------------------------------------------------
% Examples:
%   From the command line:
%     lib_unit_conversion_mask('update');
%   From the mask:
%     lib_unit_conversion_mask('viz');
%     lib_unit_conversion_mask('init');
%
% Description:
%   This function gathers all of the callbacks for the
%   lib_unit_conversion/unit conversion block, as well as a function to
%   update the parameters based on the currently available units in
%   unit_conversion.m.
% -------------------------------------------------------------------------

% Pieced together by Bob Kagy

switch cmd
    case 'update'
        iUpdateMask;
    case 'viz'
        iUpdateViz;
    case 'init'
        iInit;
end

%% ------------------------------------------------------------------------
function iUpdateMask
% Update the mask based on the information returned by unit_conversion.m

% Get the block of interest
blk = 'lib_unit_conversion/unit conversion';

% Get the list of types of units
unitTypes = unit_conversion('supported types');

% Get the list of units for each type
units = struct;
for idx=1:length(unitTypes)
    units.(unitTypes{idx}) = unit_conversion( unitTypes{idx} );
end

% --- Calculate the correct value for masks
maskPrompts = cell(3+2*length(unitTypes),1);
maskPrompts{1} = 'Gain';
maskPrompts{2} = 'Bias';
maskPrompts{3} = 'Type of units';
maskNames = cell(3+2*length(unitTypes),1);
maskNames{1} = 'gain';
maskNames{2} = 'bias';
maskNames{3} = 'selType';
maskStyles = cell(3+2*length(unitTypes),1);
maskStyles{1} = 'edit';
maskStyles{2} = 'edit';
unitTypeStr = repmat({'|'},1,2*length(unitTypes)-1);
[unitTypeStr{1:2:end}] = deal(unitTypes{:});
unitTypeStr = [unitTypeStr{:}];
maskStyles{3} = ['popup(',unitTypeStr,')'];
maskEnables = repmat({'on'},3+2*length(unitTypes),1);
maskEnables{1} = 'off';
maskEnables{2} = 'off';
maskTunableValues = repmat({'off'},3+2*length(unitTypes),1);
maskValues = cell(3+2*length(unitTypes),1);
maskValues{1} = '1';
maskValues{2} = '0';
maskValues(3) = unitTypes(1);
maskVisibilities = repmat({'off'},3+2*length(unitTypes),1);
maskVisibilities{1} = 'on';
maskVisibilities{2} = 'on';
maskVisibilities{3} = 'on';
maskCallbacks = repmat({''},3+2*length(unitTypes),1);
maskCallbacks{3} = 'lib_unit_conversion_mask(''viz'')';

for idx=1:length(unitTypes)
    % Prompts
    maskPrompts{2*idx+2} = 'Input Unit';
    maskPrompts{2*idx+3} = 'Output Unit';
    % Names
    maskNames{2*idx+2} = [unitTypes{idx},'I'];
    maskNames{2*idx+3} = [unitTypes{idx},'O'];
    % Styles
    popupStr = repmat({'|'},1,2*length(units.(unitTypes{idx})));
    [popupStr{1:2:end}] = deal(units.(unitTypes{idx}){:});
    popupStr = ['popup(',popupStr{:},')'];
    maskStyles{2*idx+2} = popupStr;
    maskStyles{2*idx+3} = popupStr;
    % Initial value
    maskValues(2*idx+2) = units.(unitTypes{idx})(1);
    maskValues(2*idx+3) = units.(unitTypes{idx})(1);
end
maskVariables = '';
for idx=1:length(maskNames)
    if idx <= 2
        typeChar = '@';
    else
        typeChar = '&';
    end
    maskVariables = sprintf('%s%s=%s%d',maskVariables,...
        maskNames{idx},typeChar,idx);
end
% --- Update masks
set_param(blk,...
    'MaskPrompts',maskPrompts,...
    'MaskVariables',maskVariables,...
    'MaskStyles',maskStyles,...
    'MaskEnables',maskEnables,...
    'MaskTunableValues',maskTunableValues,...
    'MaskVisibilities',maskVisibilities,...
    'MaskCallbacks',maskCallbacks,...
    'MaskInitialization','lib_unit_conversion_mask(''init'');',...
    'MaskSelfModifiable','on',...
    'MaskValues',maskValues );


%% ------------------------------------------------------------------------
function iUpdateViz
% Select the appropriate set of units to show

% Get information about the block/model
blk = gcbh;
names = get_param(blk,'MaskNames');
unitType = get_param(blk,'selType');

% Initialize viz
viz = repmat({'off'},length(names),1);
[viz{1:3}] = deal('on');

% Select those to show
match = regexp(names,[unitType,'(I|O)']);
for idx = 1:length(match)
    if ~isempty(match{idx})
        viz{idx} = 'on';
    end
end

% Update mask if required
pViz = get_param(blk,'MaskVisibilities');
if ~all(strcmp(pViz,viz))
    set_param(blk,'MaskVisibilities',viz);
end

%% ------------------------------------------------------------------------
function iInit
% Configure the icon and gain
blk = gcb;

% Get units
unitType = get_param(blk,'selType');
unitI = get_param(blk,[unitType,'I']);
unitO = get_param(blk,[unitType,'O']);

% Get inv/gain/offset
[inv,gain,bias] = unit_conversion(unitI,unitO);
gain = num2str(gain,30);
bias = num2str(bias,30);

% Set product block if required
if inv
    desProd = '*/';
else
    desProd = '**';
end
curProd = get_param([blk,'/Product'],'Inputs');
if ~strcmp(curProd,desProd)
    set_param([blk,'/Product'],'Inputs',desProd);
end

% Calculate mask icon
iconStr = sprintf('disp(''%s'');\nport_label(''input'',1,''%s'');\nport_label(''output'',1,''%s'');',...
    unitType,unitI,unitO);

% Set gain, offset, & mask icon if different
params = {};
cGain = get_param(blk,'gain');
if ~strcmp(cGain,gain)
    params = [params,{'gain',gain}];
end
cBias = get_param(blk,'bias');
if ~strcmp(cBias,bias)
    params = [params,{'bias',bias}];
end
cIcon = get_param(blk,'MaskDisplay');
if ~strcmp(cIcon,iconStr)
    params = [params,{'MaskDisplay',iconStr}];
end
if ~isempty(params)
    set_param(blk,params{:});
end

Contact us