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
2012 年 11 月 5 日
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
2012 年 11 月 5 日
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
Dipesh
2012 年 11 月 5 日
What do you mean by documentation for function? Just the "help function" in matlab?
José-Luis
2012 年 11 月 5 日
Yes. Type doc function or help function in the command line.
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
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
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
2012 年 11 月 5 日
There is a space missing. See the edited code.
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?
Error: it is my_value2, not myvalue2. With that correction it works for me.
Dipesh
2012 年 11 月 5 日
function[N1 N2] = my_function
N1 = rand(1); N2 = rand(1);
José-Luis
2012 年 11 月 5 日
And in your script?
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
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
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 Exchange で Call Java from MATLAB についてさらに検索
製品
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
