How can I run a matlab program N times using a different value of one of my variables
17 ビュー (過去 30 日間)
古いコメントを表示
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.
8 件のコメント
gonzalo Mier
2019 年 6 月 14 日
Thank you Adam, but I had the feeling it was not over.
mmmmm... ok, can you please add the code of the real problem or a closer example of your real problem so we have some idea about how to solve your question?
Help us to help you :)
採用された回答
per isakson
2019 年 6 月 15 日
編集済み: per isakson
2019 年 6 月 15 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!