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
採用された回答
Walter Roberson
2016 年 12 月 28 日
load(['U_', num2str(nc,'%02d'), 'Acc_TimeD_FreqD_FDay.mat']);
or
load( sprintf('U_%02dAcc_TimeD_FreqD_FDay.mat', nc) );
10 件のコメント
neamah al-naffakh
2016 年 12 月 28 日
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!
Walter Roberson
2016 年 12 月 28 日
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?
Walter Roberson
2016 年 12 月 28 日
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

neamah al-naffakh
2016 年 12 月 28 日
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
Walter Roberson
2016 年 12 月 28 日
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 についてさらに検索
参考
2016 年 12 月 28 日
2016 年 12 月 30 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
