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
Ana Gabriela Guedes 2021년 7월 16일
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에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by