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

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?

 Réponse acceptée

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 commentaires

What does res mean?
It is just a placeholder variable name:
function [res1 res2 res3] = your_function(optional_argument)
%do your stuff
your_variable_1 = %some stuff
your_variable_2 = %some other stuff
your_variable_3 = %plenty of stuff
Have you read the documentation for function?
numVals = 3;
res1 = ones(numVals,1); %preallocate!
res2 = res1; res3 = res1;
for ii = 1:numVals
[M1(ii) M2(ii) M3(ii)] = your_function(optional_argument);
end
What do you mean by documentation for function? Just the "help function" in matlab?
Yes. Type doc function or help function in the command line.
I still don't understand what to do. I have written this:
function[M1, M2] = mymfile
for ii = 1:3
mymfile
end
This just plots my Mfile and says "ans = X" where X is my last value of a variable. What am I missing out?
Like I'm missing the other stuff that you have written but I don't understand what you are saying or what it means so I don't know where to put it in or assign what to it or anything
Ok, I am going to try to illustrate how functions work like. This is a basic programming concept, so I advise you to spend some time on this, and reread the documentation. In one file, that you should save with the name "my_function", you should have this:
function [my_value1 my_value2] = my_function(my_argument)
sprintf('My argument is %d but I will not use it, as this is just an example',my_argument);
my_value1 = rand(1); %or however you generate your result.
my_value2 = rand(1);
In another m-file or in the command line do:
numVals = 3;
your_results1 = ones(numVals,1); %preallocate!
your_results2 = your_results1;
for ii = 1:numVals
[your_results1(ii) your_results2(ii)] = my_function(42);
end
Are you missing something in the line:
[your_results1(ii)your_results2(ii)
because I keep getting an error saying "Unexpected Matlab expression)
There is a space missing. See the edited code.
Ok, I did everything exactly as you wrote it and I still keep getting an error on that line. It just says :
Attempt to execute SCRIPT my_function as a function: m:\matlab32\my_function.m
Error in practicemultiple (line 6)
[your_results1(ii) your_results2(ii)] = my_function
Why? I don't see how the file "function" has any effect on this new file?
José-Luis
José-Luis le 5 Nov 2012
Modifié(e) : José-Luis le 5 Nov 2012
Error: it is my_value2, not myvalue2. With that correction it works for me.
function[N1 N2] = my_function
N1 = rand(1); N2 = rand(1);
And in your script?
numVals = 3; your_results1 = ones(numVals,1) your_results2 = your_results
for ii = 1:numVals [your_results1(ii) your_results2(ii)] = my_function end
I tried with and without the (42) at the end and I got the same message
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.
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?

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by