Skip to content
Snippets Groups Projects
Commit cd2d017e authored by Federico Felici's avatar Federico Felici
Browse files

Generalize SCDclass_algo: multi-output functions

Also allow assigning variables to either base workspace
OR assigninGlobal by Simulink
parent b7c75a2f
No related branches found
No related tags found
No related merge requests found
......@@ -213,26 +213,39 @@ classdef SCDclass_algo
end
%% Fixed inits
function obj = addfpinitfcn(obj, fcnname, targetstruct)
function obj = addfpinitfcn(obj, fcnname, targetstruct,targetworkspace)
if nargin<3
targetstruct = '';
end
if nargin<4
targetworkspace = 'global'; % default: global workspace (assigninGlobal)
end
if ~iscell(targetstruct)
targetstruct = {targetstruct};
end
if(~isempty(obj.stdinits))
for ii=1:numel(obj.stdinits)
if(strcmp(char(obj.stdinits{ii}{2}),targetstruct))
warning('SCDclass_algo:addfpinitfcn','A function driving the structure %s has already been added, ignoring.\d',targetstruct)
return;
if contains(char(obj.stdinits{ii}{2}),targetstruct)
warning('SCDclass_algo:addfpinitfcn','A function defining the structure %s has already been added, ignoring.\d',targetstruct)
return
end
end
end
% FF question to CG: why is this a cell and not a struct?
temp=cell(10,1);
temp{1}=fcnname;
temp{2}=targetstruct;
temp{3}=targetworkspace;
obj.stdinits{end+1}=temp;
end
function printinits(obj)
if(~isempty(obj.stdinits))
for ii=1:numel(obj.stdinits)
fprintf('%s -> %s\n',char(obj.stdinits{ii}{1}),char(obj.stdinits{ii}{2}));
fprintf('%s -> %s in workspace %s \n',char(obj.stdinits{ii}{1}),char(obj.stdinits{ii}{2},obj.stdinits{ii}{3}));
end
end
end
......@@ -244,29 +257,41 @@ classdef SCDclass_algo
for ii=1:numel(obj.stdinits)
initfunction = obj.stdinits{ii}{1};
targetname = obj.stdinits{ii}{2};
targetnames = obj.stdinits{ii}{2};
workspace = obj.stdinits{ii}{3};
nout = numel(targetnames);
fprintf('Calling init function ''%s'', assigning its output to ''%s'' ...\n',...
char(initfunction),char(targetname));
char(initfunction),cell2str(targetnames));
if ischar(initfunction)
initcmd=sprintf('%s(obj);', initfunction);
value = eval(initcmd);
[value{1:nout}] = eval(initcmd);
elseif isa(initfunction,'function_handle')
if nargin(initfunction)==1
value = initfunction(obj); % function has an input argument
[value{1:nout}] = initfunction(obj); % function has an input argument
elseif nargin(initfunction)==0
value = initfunction(); % no input arguments
[value{1:nout}] = initfunction(); % no input arguments
else
error('unexpected number of input arguments for function %s',func2str(initfunction));
end
else
error('initfunction must be a string or a function handle')
end
% assigns in base workspace or datadictionary depending
% on where the target variable is
Simulink.data.assigninGlobal(obj.modelname, targetname, value);
% on target workspace
for iout = 1:nout
% cycle over number of output arguments of the function
val = value{iout};
target = targetnames{iout};
if strcmp(workspace,'global')
Simulink.data.assigninGlobal(obj.modelname, target, val);
elseif strcmp(workspace,'base')
assignin('base',target,val)
end
end
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment