How can I call a script within a for loop, looping over multiple files?
15 views (last 30 days)
Show older comments
Caitlin Ridgewell
on 21 Jul 2021
Commented: Caitlin Ridgewell
on 22 Jul 2021
Hi all, I know this information must be out there somewhere but I'm just not seeing what I need or maybe I'm just not understanding what I'm reading since I'm not very MATLAB literate. Anyway, thank you for your help in advance.
I'm trying to put together a script containing a for loop, in which I can call another script and apply that second script to a number of files. The files are organized by participant ID and by visit number so in my loop script (loop.m), I have included variable identifiers:
ID = {'1','2','3'};
Visit = {'1','2'};
These IDs correspond to the IDs referenced in the script that I'm calling and have the same name as in the script that I'm calling. Once I've specified those variables, I start my for loop:
for nSubject = 1:length(ID)
for nVisit = 1:length(Visit)
result = scriptname(nSubject,nVisit));
end
end
Within the second script, here referred to as scriptname.m, the first few lines take that information and then start working with it:
function task1 = scriptname(ID,Visit)
filename = append('ECG_',ID,'_',Visit,'.txt');
filelocation = fullfile('path\',ID,'\',Visit);
cd(filelocation)
etc.
What I'm running into is that when I run loop.m, I get errors.
Error using append (line 62)
Input must be a string array, character vector, or cell array of character vectors.
Error in scriptname (line 8)
filename = append('ECG_',ID,'_',Visit,'.txt');
Error in loop (line 22)
result = scriptname(nSubject,nVisit);
I think I have a few problems:
- It looks like ID and possibly also Visit aren't creating correctly - when I run that part of the script, the resulting cells includes all the participant IDs in the origin file, even if I haven't specified those IDs.
- I'm probably calling the script incorrectly in the for loop. I know that scriptname.m works just fine when I specify ID and Visit alone and then just run scriptname.m - so it sounds to me like loop.m is the problem, not necessarily scriptname.m.
Any suggestions for me? THANK YOU
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!