I have 3 scripts.
a= 'add.m'
b ='split.m'
c ='find.m'
I want to run those scripts in sequence, how can i do it?
I used the for loop but it didn't work.
d = [ a , b , c]
for i =1: 3
try
run(d(i)
catch
disp (' it does not work')
end
end

 Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 15 Nov 2023
First of all, Never use built-in functions as variable or scripts names.
You should modify the names of the scripts; for e.g. by adding a prefix as I have done below (or a suffix), or change the names entirely.
Second, you need to store the names as strings or cell array of char vectors -
a = 'script_add.m';
b = 'script_split.m';
c = 'script_find.m';
%Check the output of concatenating the names directly
d = [a b c]
d = 'script_add.mscript_split.mscript_find.m'
%First three elements
d(1:3)
ans = 'scr'
%Store in cell array
names = {a, b, c};
for k=1:numel(names)
try
run(names{k})
catch
disp('It does not work')
end
end

6 commentaires

Antonio
Antonio le 15 Nov 2023
I tried your code but it runs only the first elements of names.
Brace indexing is not supported for variables of this type. Assign the result of 'names' to a variable first, then brace index into it
Dyuman Joshi
Dyuman Joshi le 15 Nov 2023
Could you share the code you ran?
Antonio
Antonio le 15 Nov 2023
script1= "Report_1.m";
script2= "Report_2.m";
script3="Report_3.m";
script4="Report_4.m";
script5="Report_5.m";
script = {script1,script2,script3,script4,script5};
for i = 1:5
b=(script{i})
run(b)
end
Brace indexing into the result of a function call is not supported. Assign the result of 'script' to a
variable first, then brace index into it.
Error in a (line 10)
b=(script{i})
Dyuman Joshi
Dyuman Joshi le 15 Nov 2023
I ran this code on my PC with different scripts and it works.
Are any of the scripts defined as a function?
Could you attach all of the files here? Use the paperclip button to attach.
Antonio
Antonio le 15 Nov 2023
They are not defined as a function
Dyuman Joshi
Dyuman Joshi le 15 Nov 2023
Could you attach the files, so I can reproduce the error and provide you with a working solution?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!