trouble with the eval function and cell
11 ビュー (過去 30 日間)
古いコメントを表示
Hi guys,
AIM OF FUNCTION
First I need to read out ~100 .mat files with preprocessed data, each containing around 50 variables, a wild mixture of matrices, stings and structs. No problem so far. I then want to store all variables from a file in a cell array.
Example:
load processeddata3.mat
varList = who;
for i=1:+1:size(varList,1)
s = ['data{ex.no,1}.' varList(i) ' = ' varList(i)]
eval(s)
end
where data is the name of the cell array I wish to create, ex.no is the interal number of a dataset.
The string looks like that:
s = 'data{ex.no}.' 'AfterSpikes' ' = ' 'AfterSpikes'
If I type it in by hand it works perferctly, eval gives the error message
??? Undefined function or method 'eval' for input arguments of type 'cell'.
Any idea how to solve this, or an idea for a workaround? Help would really be appreciated.
Thanks, Alex
0 件のコメント
回答 (2 件)
Andrew Newell
2012 年 1 月 20 日
Avoid eval - use dynamic field names:
data{ex.no,1}.(varList{i}) = varList{i};
A simpler approach is to use
s = load('processeddata3.mat');
EDIT: Modified in view of Walter's comment and discussion below
2 件のコメント
Andrew Newell
2012 年 1 月 21 日
編集済み: per isakson
2017 年 2 月 16 日
Oh! You can do that in a single command:
S = load('processeddata3.mat');
As for cells, one nice use for them is to store an array of strings of different lengths - and see also varargin.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!