Loop multiple files from a cell
3 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to get the nanmean from my data. Firstly I load my files (.mat) into a cell (files_mat). Now files_mat is (19x1) and each cell has a matrix of (10200x13). I would like to get the nanmean from all matrices and each matrix has different names, so I am doing a Loop and the resultas is going to be in a new matrix (res_est).
res_est = NaN(19,13)
for i=1:length(files_mat)
res_est(k,13)=nanmean(files_mat{k,1}.Matriz_A(:,1:13)
end
I get the result for the first matrix, but I would ike to get the nanmean of all matrices successively, the problem is: that I have to change manually the name.
for i=1:length(files_mat)
res_est(k,13)=nanmean(files_mat{k,1}.Matriz_B(:,1:13)
end
Some advice to get the nanmean from all files? Thanks
0 个评论
采纳的回答
Walter Roberson
2020-8-29
Currently your loop to load looks somethng like,
dinfo = dir('*.mat');
nfiles = length(dinfo);
files_mat = cell(nfiles, 1);
for K = 1 : nfiles
filename = dinfo(K).name;
files_mat{K} = load(filename);
end
You can instead use
dinfo = dir('*.mat');
nfiles = length(dinfo);
files_mat = cell(nfiles, 1);
for K = 1 : nfiles
filename = dinfo(K).name;
data_struct = load(filename);
fn = fieldnames(data_struct);
files_mat{K} = data_struct.(fn{1});
end
Now files_mat will be a cell of arrays instead of cell of struct.
If all of the arrays are the same size you could use a numeric array instead of a cell:
dinfo = dir('*.mat');
nfiles = length(dinfo);
files_mat = zeros(0,0,nfiles);
for K = 1 : nfiles
filename = dinfo(K).name;
data_struct = load(filename);
fn = fieldnames(data_struct);
this_data = data_struct.(fn{1});
files_mat(1:size(this_data,1), 1:size(this_data,2), K) = this_data;
end
and after that you can nan_mean across entire sections:
res_est(:,13) = nanmean(files_mat, [1 2]);
Notice here that I take the mean over the first two dimensions. Your code
for i=1:length(files_mat)
res_est(k,13)=nanmean(files_mat{k,1}.Matriz_B(:,1:13)
end
has a confusion between i and k, and is taking the nan_mean() over 10200 rows by 13 columns but assigning to a scalar location, so we can guess that you want to take the mean of everything rather than a row or column mean.
Also since your array has 13 columns and you are accessing (:,1:13) then the (:,1:13) is redundant, which is why I left off the indexing from files_mat.
3 个评论
Walter Roberson
2020-8-29
If you use the version I showed with loading into a numeric array, then you would be able to use
stad_mean = squeeze(nanmean(files_mat, 1));
I though the problem was that I was getting the data to a struct and not to a double
Yes, that was the problem. In particular, you had a struct with a field name that was the same as the name of the variable stored in the file, because when you call load() with an output, it creates a struct with one field for every variable in the file. fieldnames() asks what names were received, and the syntax STRUCTURE.(EXPRESSION) dynamically evaluates EXPRESSION to find the name of the field to access. STRUCTURE.(EXPRESSION) is a special syntax in MATLAB.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!