Asked by Kareemmasud
on 14 Jun 2019

Imagine a scenerio where I have a matlab program with N variables. I then want to make one of the variables a vector so I can run the program many times (length of the vector). I just intend to do this by calling the program in a loop. But each time I do that it's indicating that the variable be defined in the original program.

Answer by per isakson
on 15 Jun 2019

Edited by per isakson
on 15 Jun 2019

Accepted Answer

Assumptions:

- You have a script. Lets call it myScript.
- The first line of myScript is clear all
- Near the top of myScript there is an assignment, e.g input = 17;.
- In myScript a result is assigned to a variable, e.g. output = expression;
- Now you want to run myScript with different values, e.g. [2,3,5,7,11,13,17], of the variable, input

Procedure

- In myScript comment out the lines clear all and input = 17; Result: % clear all and % input = 17;
- Create a script in a new file, e.g. loop_over_myScript

%% loop_over_myScript

vector = [2,3,5,7,11,13,17];

out_vector = [];

for vec = vector

input = vec;

myScript

% ignore the warning about growing in the loop

out_vector = [ out_vector, output ];

end

- Beg that no variables named, vector, vec or out_vector, exist in myScript. (Renaming them vector__, vec__ and out_vector__ might decrease the risk of name collision.)
- Run loop_over_myScript
- Inspect out_vector, e.g with the Variables Editor
- Run whos and be horrified by all the variables that litters the base workspace.

Kareemmasud
on 18 Jun 2019

Thanks @Per Isakson. The program did exactly what I wanted.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 8 Comments

## per isakson (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714682

## Kareemmasud (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714686

## Jan (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714692

## Kareemmasud (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714702

## gonzalo Mier (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714708

## Adam Danz (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714757

## Kareemmasud (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714784

## gonzalo Mier (view profile)

## Direct link to this comment

https://kr.mathworks.com/matlabcentral/answers/467061-how-can-i-run-a-matlab-program-n-times-using-a-different-value-of-one-of-my-variables#comment_714924

Sign in to comment.