Apply script to multiple data files and save specific workspace outputs

2 次查看(过去 30 天)
Hi!
I have a script to analyse ECG that I run and gives me multiple outputs, from which I want to save a specific one with a specific name.
So, I run the script, that starts loading the data file like this, for example:
%::::::::::::::::::::::::::::::::::::::::::::::::::::: Path Initialization
rootCV=[char(pwd) '\'];
ecgPathConfig(rootCV);
display =0;
% maximise =OPTION.M;
% default=0;
% datPath = [rootCV 'ECG_DAT\MIT\' ]; % path
% datFile = 'cu01.dat'; % file name
%==========================================================================
%::::::::::::::::::::::::::::::::::::::::::::::::::: Load and PreProcessing
%
%¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨ Data File
signal = load('EKG_AB16_D1.txt'); %Example of the name of a file
DAT.Path = 'C:\Users\myPath';
DAT.File = 'EKG_AB16_D1.txt';
DAT.Format = 0; DAT.Samp = 500; DAT.Ini = 1; DAT.ECG = signal;
%From here there is the code itself
It will gives me multiple outputs and I want to save a struct named WAVES with the name WAVES_AB16_D1.
The thing is that I have a lot of participants I want to analyse. The .txt files have always the following name format:
EKG_AB16_D1.txt , EKG_AB16_D2.txt, EKG_AB16_G1.txt, EKG_AB16_G2.txt, EKG_AB17_D1.txt, EKG_AB17_D2.txt, EKG_AB17_G1.txt, EKG_AB17_G2.txt , etc
So I want to save the correspondent data file WAVES with the name of the partipant in the front. Is it possible to do this all at once or I have to do it manually?

采纳的回答

Hrishikesh Borate
Hrishikesh Borate 2021-7-16
Hi,
Assuming that the EKG_AB16_D1.txt, EKG_AB16_D2.txt, etc. files are stored in a directory called inputData, following code demonstrates loading these files and saving the output in the desired format.
inputDataPath = fullfile(pwd,'inputData');
inputDataFiles = dir(fullfile(inputDataPath, '*.txt'));
for i=1:length(inputDataFiles)
signal = load(fullfile(inputDataFiles(i).folder, inputDataFiles(i).name));
% Perform processing on the signal
old = {'EKG','txt'};
new = {'WAVES','mat'};
outputFileName = replace(inputDataFiles(i).name,old,new);
save(outputFileName, 'WAVES');
end
For more information, refer replace.
  1 个评论
Ana Gabriela Guedes
Thank you!!
Just 2 more questions:if I can save an image gaven by the script to each file how can I do it?
And if I want to save more than 1 output I just have to add it to new = {'WAVES','mat'} ?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by