Saving the outputs of multiple runs of a script

2 views (last 30 days)
I have a stochastic simulation which I would like to run multiple times while changing a variable called 'correlation'.
For each run I would like to save the outputs as individual matrices so I can plot them together and compare values.
How would I go about saving the outputs of each simulation for each corresponding value of correlation?
I tried incorporating this into a 'for' statement but I'm very new to MATLAB and get the error "Array indices must be positive integers or logical values."
My code so far is:
R01= zeros(length(t),maxit);
R02= zeros(length(t),maxit);
R03= zeros(length(t),maxit);
R04= zeros(length(t),maxit);
R05= zeros(length(t),maxit);
R06= zeros(length(t),maxit);
R07= zeros(length(t),maxit);
R08= zeros(length(t),maxit);
R09= zeros(length(t),maxit);
for correlation=(0.1:0.1:0.9)
run stochasticsimulation.m
ResultsR01(:,correlation)= R01;
ResultsR02(:,correlation)= R02;
ResultsR03(:,correlation)= R03;
ResultsR04(:,correlation)= R04;
ResultsR05(:,correlation)= R05;
ResultsR06(:,correlation)= R06;
ResultsR07(:,correlation)= R07;
ResultsR08(:,correlation)= R08;
ResultsR09(:,correlation)= R09;
end
Could you please advise on either where to look to learn this stuff or what improvements I can make to my code.
Thanks!

Accepted Answer

VBBV
VBBV on 17 May 2021
%if
Count = 0.1:0.1:0.9;
for correlation=1:length(Count)
Change this line in for loop and run it.
  2 Comments
Rohan Soni
Rohan Soni on 17 May 2021
Thank you for your submission! I tried doing this but then received an error saying that the indices of left side not compatible with size of right side?
Not sure why this is happening but I feel like it may be to do with the way the script I'm calling is outputting results.
Rik
Rik on 17 May 2021
@VBBV You should avoid using length. It is never the best choice. Either use numel or size with a dimension.

Sign in to comment.

More Answers (1)

Rik
Rik on 17 May 2021
You should not be using a script outside of debugging. If you're only interested in the value of a single variable, you should make your script a function with that variable as the output. Then you can run it in a loop.
You should also note that indices in an array must be positive integers:
my_array(0.1) % this is an invalid syntax
my_array(1) % this is a valid syntax
You should also avoid numbering your variables, use indexing instead. If you only store a single value:
%change this
R01=my_function;
R02=my_function;
R03=my_function;
%to this:
R(1)=my_function;
R(2)=my_function;
R(3)=my_function;

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!