evalin('base', 'save('var1', 'var2')')
古いコメントを表示
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;
採用された回答
その他の回答 (1 件)
Fangjun Jiang
2011 年 12 月 28 日
0 投票
You need to pay attention to the syntax for using single quote inside the single quote. You could do.
evalin('base', 'save(''MyMatFile'', ''var1'')')
Note the first input argument for save() is the file name, not a variable name.
6 件のコメント
Scragmore
2011 年 12 月 28 日
Fangjun Jiang
2011 年 12 月 28 日
So what is the problem? As long as you specify the correct arguments for save(), it should work without the problem. You could try a simple example first.
Scragmore
2011 年 12 月 28 日
Fangjun Jiang
2011 年 12 月 28 日
If it is about file name, use movefile(), copyfile() or even the old fashion !ren
Scragmore
2011 年 12 月 28 日
Fangjun Jiang
2011 年 12 月 28 日
There might be different ways to do it. But I am thinking since the variables are in base workspace, you might just run evalin('base','save(...)') and save it to a temporary file name. Then you can use movefile() to rename the file name according to the string name inside your function.
カテゴリ
ヘルプ センター および File Exchange で Variables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!