view m-toolbox/classes/@ssm/getMatrixSelection.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents f0afece42f48
children
line wrap: on
line source

% GETMATRIXSELECTIONS allows to select data in a block-defined matrix 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: GETMATRIXSELECTIONS  allows to select data in a
% block-defined matrix with user i/o selection, and i/o data selection.
% Permuations matrices are returned
%
% CALL: [ao_out pl_out] = getModelMatrixSelection(sys, plist_inputs)
%
% INPUTS:
%
% OUTPUTS:
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'getModelMatrixSelection')">Parameters Description</a>
%
% VERSION: $Id: getMatrixSelection.m,v 1.5 2011/04/08 08:56:23 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = getMatrixSelection(blockMat, oldColumns, newColumns, oldRows, newRows)
  error('This function is deprecated, it will be modified or removed. Use double instead.')
  utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
  
  %% making indexes out of i/o data
  indexInputsIn = makePortIndex( oldColumns, newColumns );
  colSizesIn = getsizes(indexInputsIn);
  colSizes = oldColumns.Nports;
  
  indexOutputsOut = makePortIndex( oldRows, newRows );
  rowSizesOut = getsizes(indexOutputsOut);
  rowSizes = oldRows.Nports;
  
  %% pruning ssmBlocks
  inputNamesFull = getcellstr(oldColumns, colSizes);
  inputsIn = blocksPrune(oldColumns, indexInputsIn);
  inputNames = getcellstr(inputsIn, colSizesIn);
  inputUnits = getUnit(inputsIn, colSizesIn);
  
  outputNamesFull = getcellstr(oldRows, rowSizes);
  outputsOut = blocksPrune(oldRows, indexOutputsOut);
  outputNames = getcellstr(outputsOut, rowSizesOut);
  outputUnits = getUnit(outputsOut, rowSizesOut);

  %% pruning matrix and turning it to a double
  A = ssm.blockMatFusion( blockMat, rowSizes, colSizes);
  
  %% finding input and output variable positions of data in option plists
  
  if ischar(newColumns) && strcmp(newColumns, 'ALL') %case 'ALL' 'NONE'
    inputsConv = eye(sum(colSizes),sum(colSizes));
  elseif ischar(newColumns) && strcmp(newColumns, 'NONE') %case 'NONE'
    inputsConv = eye(sum(colSizes),0);
  else
    if ischar(newColumns)
      newColumns = {newColumns};
    end
    NinputsIn = numel(newColumns);
    Ninputs = sum(colSizes);
    inputsConv = zeros(Ninputs,NinputsIn);
    for i=1:NinputsIn
      position = reshape( strcmpi(inputNamesFull, newColumns{i}) , [Ninputs, 1]);
      inputsConv(:,i) = double(position).';
      if sum(position)==0
        error(['No variable found named ', newColumns{i}]);
      end
    end
  end
  
  %% finding input and output variable positions of data in option plists
  
  if ischar(newRows) && strcmp(newRows, 'ALL') %case 'ALL' 'NONE'
    outputsConv = eye(sum(rowSizes),sum(rowSizes));
  elseif ischar(newRows) && strcmp(newRows, 'NONE') %case  'NONE'
    outputsConv = eye(0,sum(rowSizes));
  else
    if ischar(newRows)
      newRows = {newRows};
    end
    NoutputsOut = numel(newRows);
    Noutputs = sum(rowSizes);
    outputsConv = zeros(NoutputsOut,Noutputs);
    for i=1:NoutputsOut
      position = reshape( strcmpi(newRows{i}, outputNamesFull ) , [1, Noutputs]);
      outputsConv(i,:) = double(position);
      if sum(position)==0
        error(['No variable found named ', newRows{i}]);
      end
    end
  end
  
  %% parsing output matrices
  varargout = {A, inputsConv, outputsConv, inputNames, outputNames, inputUnits, outputUnits};
end

function sizes = getsizes(idx)
 sizes = zeros(1, numel(idx));
 for i=1:numel(idx)
 sizes(i) = sum(idx{i});
 end
end

function names = getcellstr(block, sizes)
 names = cell(1, sum(sizes));
 k = 1;
 for i=1:numel(block)
 for j=1:sizes(i)
 names{k} = block(i).ports(j).name;
 k = k+1;
 end
 end
end

function units = getUnit(block, sizes)
 units = unit.initObjectWithSize(1, sum(sizes));
 k = 1;
 for i=1:numel(block)
 for j=1:sizes(i)
 units(k) = block(i).ports(j).units;
 k = k+1;
 end
 end
end