load multiple .mat files
显示 更早的评论
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.
neamah al-naffakh
2016-12-28
编辑:neamah al-naffakh
2016-12-28
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.
neamah al-naffakh
2016-12-28
编辑:neamah al-naffakh
2016-12-28
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
neamah al-naffakh
2016-12-28
编辑:neamah al-naffakh
2016-12-28
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 !!

neamah al-naffakh
2016-12-28
编辑:neamah al-naffakh
2016-12-28
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)
neamah al-naffakh
2016-12-30
编辑:neamah al-naffakh
2016-12-30
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
