hi everyone,
I have a question.
I am analysing some data which have multiple subjects. Information about every subject is stored in the MATLAB structure (.mat file)
For Example:
U1_Acc_TimeD_FreqD_FDay.mat
U2_Acc_TimeD_FreqD_FDay.mat
U1_Acc_TimeD_FreqD_FDay.mat
and so on...
I would like to create for loop which in every iteration load new data_number.mat file.
I'd like in every iteration to load next .mat file? Particularly, I don't know how to address number part of filename using load function.
I tried this code and seems it's overwritten the data!
clear;
for nc = 1:36
load(['U', num2str(nc,'%2d'), '_Acc_TimeD_FreqD_FDay.mat']);
end
Thank you

 采纳的回答

load(['U_', num2str(nc,'%02d'), 'Acc_TimeD_FreqD_FDay.mat']);
or
load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );

10 个评论

dear Walter Roberson, thank you very much. Just a small correction, if you don't mind because your code gives a simple error due to the file name
for nc = 1:36
load(['U', num2str(nc,'%2d'), '_Acc_TimeD_FreqD_FDay.mat']);
end
Again really your answer was so useful. thank you mate.
dear Walter Roberson, this code is uploading only the one mat file!! could you help me please? seems is data is overwritten!
Your question did not ask for the data to be stored separately.
for nc = 1 : 36
data{nc} = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
end
This will give you a cell array of structures, with each structure containing one field for each variable that is stored in the .mat file.
If you already know the name of the variable you want to extract, then,
for nc = 1 : 36
file_data = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
data{nc} = file_data.NameOfVariableGoesHere;
end
This code does not assume that the size or data type of the stored information is all the same. If you know that it is vectors that are consistent length, then you could use
for nc = 1 : 36
file_data = load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
data(:,nc) = file_data.NameOfVariableGoesHere;
end
after which the data would appear as columns, one column for each file.
Dear Walter Roberson,
I really appreciate your kind help.
I am trying to access each Struct inside the cell (data) and extract specific rows and columns.
I have written this code but it stores the data of the last Struct only!
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=size(data,2)
Feat_Vec1=(data{j}.Acc_TD_Feat_Vec(2:36,1:126));
end
could you help me with this please?
for j = 1 : size(data,2)
Feat_Vec1(:,:,j) = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
This would create a 35 x 126 x 36 array
thank you so much, but actually, it doesn't give me the correct output!
*it converts the values of each struct into 0 :(*
please see
when I use this code, I get what I want but only for the last struct (36) in the cell data
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=size(data,2)
Feat_Vec1{j} = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
please have a look on here !!
I have solved it but it's really weird !!
clear;
for nc = 1 : 36
data{nc} = load( sprintf('U%02d_Acc_TimeD_FreqD_FDay.mat', nc) );
end
for j=1:36 *% instead of for j=size(data,2)*
Feat_Vec1{j} = data{j}.Acc_TD_Feat_Vec(2:36,1:126);
end
Notice I had suggested
for j = 1 : size(data,2)
rather than
for j = size(data,2)
the second of those does only size(data,2)
dear Walter Roberson, many thanks for your help. I really appreciate it.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by