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?

 採用された回答

José-Luis
José-Luis 2012 年 11 月 5 日

0 投票

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 件のコメント

Dipesh
Dipesh 2012 年 11 月 5 日
What does res mean?
José-Luis
José-Luis 2012 年 11 月 5 日
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
Dipesh
Dipesh 2012 年 11 月 5 日
What do you mean by documentation for function? Just the "help function" in matlab?
José-Luis
José-Luis 2012 年 11 月 5 日
Yes. Type doc function or help function in the command line.
Dipesh
Dipesh 2012 年 11 月 5 日
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
José-Luis
José-Luis 2012 年 11 月 5 日
編集済み: José-Luis 2012 年 11 月 5 日
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
Dipesh
Dipesh 2012 年 11 月 5 日
Are you missing something in the line:
[your_results1(ii)your_results2(ii)
because I keep getting an error saying "Unexpected Matlab expression)
José-Luis
José-Luis 2012 年 11 月 5 日
There is a space missing. See the edited code.
Dipesh
Dipesh 2012 年 11 月 5 日
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 2012 年 11 月 5 日
編集済み: José-Luis 2012 年 11 月 5 日
Error: it is my_value2, not myvalue2. With that correction it works for me.
Dipesh
Dipesh 2012 年 11 月 5 日
function[N1 N2] = my_function
N1 = rand(1); N2 = rand(1);
José-Luis
José-Luis 2012 年 11 月 5 日
And in your script?
Dipesh
Dipesh 2012 年 11 月 5 日
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
José-Luis
José-Luis 2012 年 11 月 5 日
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 2012 年 11 月 6 日
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?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCall Java from MATLAB についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by