MATLAB: Evalin(‘base’, ‘save(‘var1’, ‘var2′)’)

evalin

evalin('base', 'save('var1', 'var2')')
I am trying to run a simple function to save specific variables from the base.workspace to a specific location. The variable to be saved and the file name are stored in arrays within the function. As I am learning you can't save base.workspace from the function and you can't send function variables to the base.workspace with 'evalin'.
What is the best way to save base.workspace.array's from a function when the save variables are stored within the function. Do I need to create temp variable on the base.workspace, (I found 'assignin' or 'putvar') then remove. This needs to executed in a loop as my function cycles through the variables saving the relevant files, names to specific locations.
Full Code; NB fOut only used to check function variables are correct.
function fOut = saveData(filterIn)
%to save arrays in work space to C:\Users\Remote\Documents\MATLAB\DATA Files
cd('C:\Users\Remote\Documents\MATLAB\DATA Files');
cd1 = cd;
wSpace = evalin('base', 'whos');
arNames = cell(length(wSpace),1);
arNames = arrayfun(@(x)x.name(1,:), wSpace, 'uniformoutput', false);
if nargin >0
tempIdx = strfind(arNames, filterIn);
index = find(not(cellfun('isempty', tempIdx)));
else
index = find(not(cellfun('isempty', arNames)));
end
for iii = 1:length(index)
iiiTicker(iii,1) = arNames(index(iii,1),:);
iiiTickerChar{iii,1} = char(arNames(index(iii,1),:));
evalin('base', 'save(char(arNames(index(iii,1),:)), char(arNames(index(iii,1),:)))');
end
cd('C:\Users\Remote\Documents\MATLAB');
fOut.cd1 = cd1;
fOut.index = index;
fOut.filterIn = filterIn;
fOut.wSpace = wSpace;
fOut.arNames = arNames;
fOut.tempIdx = tempIdx;
fOut.index = index;
fOut.iii = iiiTicker;
fOut.iiiTickerChar = iiiTickerChar;

Best Answer

The command looks nicer when written as:
evalin('base', 'save(arNames{index(iii,1),:}, arNames{index(iii,1),:})')
This will work if arName and iii is defined in the base-workspace as well as the variable with the name called arNames{index(iii,1),:}. But I assume, these variables are defined in the local workspace. Therefore I stringly recommend not to use evalin for such spoofing tricks.
[EDITED]: To create the command string, when arNames, index and iii are defined locally:
name = arNames{index(iii)};
evalin('base', sprintf('save(''%s'', ''%s'')', name, name));
As usual for eval and its evil brothers, enclose this command in a try, catch block and verify that name is a valid symbol, e.g. by:
if strcmp(genvarname(name), name) == 0
error('author:toolbox:badSymbol', ...
'Bad symbol: [%s]', name);
end
This will reduce the risk of a name like "system('format D:')". I do not think, that you use such names. But bugs happen. And by definition bugs are unexpected and have unexpected results. Then it is surely a wise idea to let them not use EVAL/EVALIN as a gun to shoot up your computer. However, avoid EVAL/EVALIN would even be smarter and it is always possible.