Loop multiple files from a cell

조회 수: 1 (최근 30일)
Fabian Moreno
Fabian Moreno 2020년 8월 29일
댓글: Fabian Moreno 2020년 8월 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

채택된 답변

Walter Roberson
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
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.
Fabian Moreno
Fabian Moreno 2020년 8월 30일
Thank you again Walter :D, both given solutions worked good.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by