Load multiple matlab data files and convert them to text

1 回表示 (過去 30 日間)
Ana Gabriela Guedes
Ana Gabriela Guedes 2021 年 7 月 4 日
編集済み: Image Analyst 2021 年 7 月 4 日
Hi!
I have multiple matlab data files with different names (with ECG data from multiple patients) and I want to load them all and convert them to text.
Also, the data is in structure form so I have to do the following for each file:
signal = load('FileName')
data = [signal.val];
writematrix(data,'FileName.txt','Delimiter',',');
How can I do it all at the same time? Thank you a lot in advance :)

採用された回答

Image Analyst
Image Analyst 2021 年 7 月 4 日
Code samples for processing a sequence of files are given in the FAQ:
  6 件のコメント
Ana Gabriela Guedes
Ana Gabriela Guedes 2021 年 7 月 4 日
Here it is!
Image Analyst
Image Analyst 2021 年 7 月 4 日
編集済み: Image Analyst 2021 年 7 月 4 日
myFolder = pwd; %'C:\Myfoldersdirectory';
filePattern = fullfile(myFolder, 'EKG*.mat');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading input file : "%s".\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
storedStructure = load(fullFileName);
% See if it has a val field stored in the .mat file.
if ~isfield(storedStructure, 'val')
% Show what fieldnames there are:
fieldnames(storedStructure)
errorMessage = sprintf('Error: no field called "val" in file\n"%s".\n\nSkipping this file.', baseFileName);
uiwait(errordlg(errorMessage));
continue; % Skip to bottom of for loop and continue iterations.
end
data = -storedStructure.val;
fprintf('Now plotting %d values of data.\n', length(data));
plot(data, '-', 'LineWidth', 2);
hold on
% Create the output file name.
[~, baseFileNameNoExt, etc] = fileparts(baseFileName);
fullOutputFileName = fullfile(myFolder, [baseFileNameNoExt, '.txt']);
fprintf('Now writing output file : "%s".\n', fullOutputFileName);
writematrix(data, fullOutputFileName, 'Delimiter', ',');
end
I added some improvments and robustness checks, like printing out informational messages, adding comments, using descriptive variable names, plotting the data, and checking to see if the data is actually in the mat file to begin with. It never hurts to be extra robust.

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 4 日
If you are talking about series of files (e.g: P1.txt, P2.txt, ...) to load and convert their contents, then this might be an option:
for ii = 1:numel(Files)
FName = strcat('P', num2str(ii), '.txt');
signal = load(FName)
data = [signal.val];
writematrix(data,'FileName.txt','Delimiter',',', 'WriteMode','append');
end
  1 件のコメント
Ana Gabriela Guedes
Ana Gabriela Guedes 2021 年 7 月 4 日
Thank you!! But unfortunately the files I want to load are not in a series, so I cant use that :(
I'm doing this but how can I save each file with a different name (I want them to have the original name but in .txt format).
I'm trying to do this but it doesn't work:
myFolder = 'C:\Users\myfoldersdirectory';
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
signal = load(fullFileName);
data = -[signal.val];
writematrix(data,'fullFileName.txt','Delimiter',',');
end
I know when I write 'fullFileName.txt' it creates a file with literaly the name 'fullFileName' but how can I change that so each file in the cycle has its own original name?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by