How can I call a script within a for loop, looping over multiple files?
Show older comments
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
5 Comments
Matt J
on 21 Jul 2021
Note that you are confusing the terminology "script" with "functions",
Note that the point of FULLFILE is that it correctly handles the filepath delimiter, so this:
fullfile('path\',ID,'\',Visit);
should be replaced with this:
fullfile('path',ID,Visit);
Also note that using CD programmatically is slow and rarely (i.e. almost never) required. It is certainly not required to import/export from data files, because all MATLAB functions accept absolute/relative filepaths. Rather than slow and difficult-to-debug CD you should just generate a suitable filename (with absolute/relative filepath).
And as Matt J pointed out, you are calling a function, not a script. The differences are quite significant:
Caitlin Ridgewell
on 22 Jul 2021
Edited: Caitlin Ridgewell
on 22 Jul 2021
Yes, it is complaining that you've written the function,
function task1 = scriptname(ID,Visit)
declaring that an output variable called task1 will be returned, but task1 is never created anywhere within the body of scriptname.
Caitlin Ridgewell
on 22 Jul 2021
Accepted Answer
More Answers (0)
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!