Collecting all the data after running an M-file multiple times

8 views (last 30 days)
I used:
for ii = 1:3
mymfile
end
To run my M-file 3 times. I then get 3 values (specified in the M-file as M1 and M2) as lets say:
M1 = a1, M2=b1
M1 = a2, M2 = b2
M1 = a3, M2 = b3
But then, when I tried an graph these numbers, it only put one point on the graph (a3, b3). I checked this by doing L = numel(M2) and instead of giving me 3, it said 1, proving that Matlab is only looking at the last values.
How do I get it to take into consideration all the 3 values for running this file?

Accepted Answer

José-Luis
José-Luis on 5 Nov 2012
Make your m-file a function and return the values you want to save. Look at
doc function
Quick example:
function [res1 res2 res3] = your_function(optional_argument)
%do your stuff
res1 = %some stuff
res2 = %some other stuff
res3 = %plenty of stuff
And then in your script:
numVals = 3;
res1 = ones(numVals,1); %preallocate!
res2 = res1; res3 = res1;
for ii = 1:numVals
[res1(ii) res2(ii) res3(ii)] = your_function(optional_argument);
end
  15 Comments
José-Luis
José-Luis on 5 Nov 2012
Please edit your posts for clarity
numVals = 3;
your_results1 = ones(numVals,1);
your_results2 = your_results1;
for ii = 1:numVals
[your_results1(ii) your_results2(ii)] = my_function
end
That should work. Have you saved my_function.m? If it is a function, it will only be updated once it is saved.
Dipesh
Dipesh on 6 Nov 2012
I saved it as an .m yes. Ok, I will try this again when I get to uni today. What is supposed to happen when I run this btw, will it just collect all the data together and so I use a similar thing for my problem?

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!