フィルターのクリア

How to extract one data from various mat files to one txt file

12 ビュー (過去 30 日間)
Anthony
Anthony 2022 年 6 月 28 日
コメント済み: Anthony 2022 年 6 月 28 日
My code generates various mat files. I know how to extract one specific data from mat to txt :
Data = load('data001.mat','Vol');
DataField = fieldnames(Data);
dlmwrite('data.txt', Data.(DataField{1}));
But I do not know how to extract the same 'Vol' data of different data00x.mat to the same data.txt.
Thank you for your help.

採用された回答

Karim
Karim 2022 年 6 月 28 日
編集済み: Karim 2022 年 6 月 28 日
assuming you want to append the data to the same text file, you can generate the name of the file in a loop:
numFiles = 10;
for i = 1:numFiles
currFile = sprintf( 'data%03d.mat', i)
end
currFile = 'data001.mat'
currFile = 'data002.mat'
currFile = 'data003.mat'
currFile = 'data004.mat'
currFile = 'data005.mat'
currFile = 'data006.mat'
currFile = 'data007.mat'
currFile = 'data008.mat'
currFile = 'data009.mat'
currFile = 'data010.mat'
so the full routine would be something like (note that it won't work here since the data files are not attached)
numFiles = 10;
for i = 1:numFiles
currFile = sprintf( 'data%03d.mat', i);
Data = load(currFile,'Vol');
DataField = fieldnames(Data);
dlmwrite('data.txt', Data.(DataField{1}), '-append');
end
Error using load
Unable to find file or directory 'data001.mat'.

その他の回答 (1 件)

Nitanshu
Nitanshu 2022 年 6 月 28 日
Hi Anthony
Probably you could take a reference from below code.
% where directory name is the name of directory where all your mat files
% are present.
directory_instance = dir('directory_name');
file_names = {directory_instance.name};
[row_size, col_size] = size(file_names);
for i = 1: col_size
file_name = string(file_names(1, i))
Data = load(file_name,'Vol');
DataField = fieldnames(Data);
dlmwrite('data.txt', Data.(DataField{1}));
end
Hope it helps!

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by