Collecting all the data after running an M-file multiple times
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
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
Dipesh
on 5 Nov 2012
What does res mean?
José-Luis
on 5 Nov 2012
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
on 5 Nov 2012
What do you mean by documentation for function? Just the "help function" in matlab?
José-Luis
on 5 Nov 2012
Yes. Type doc function or help function in the command line.
Dipesh
on 5 Nov 2012
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
Dipesh
on 5 Nov 2012
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
on 5 Nov 2012
There is a space missing. See the edited code.
Dipesh
on 5 Nov 2012
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?
Error: it is my_value2, not myvalue2. With that correction it works for me.
Dipesh
on 5 Nov 2012
function[N1 N2] = my_function
N1 = rand(1); N2 = rand(1);
José-Luis
on 5 Nov 2012
And in your script?
Dipesh
on 5 Nov 2012
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
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
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?
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Products
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)