Return multiple results from a function in one variable
23 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2017 年 8 月 11 日
I have a function called "ols2" that calculates 12 different values. Is there some way I can call the function program without listing all 12 of the return values? In other words, I want to avoid having to say
[p1,p2,p3 p4,p5,p6,p7,p8,p9,p19,p11,p12] = ols2(x,y)
採用された回答
MathWorks Support Team
2020 年 9 月 30 日
編集済み: MathWorks Support Team
2020 年 9 月 30 日
One way to return multiple variables from a function is to group them in a struct. Here is an example function:
function res = foo()
res.x=1;res.y = 2;
end
You can then call the function by writing
>> my_res = foo()
and access the variables using a dot, e.g., "my_res.x". For more information about 'struct' please see our documentation page below:
0 件のコメント
その他の回答 (1 件)
Stephen23
2020 年 9 月 30 日
編集済み: Stephen23
2020 年 9 月 30 日
"I have a function called "ols2" that calculates 12 different values. Is there some way I can call the function program without listing all 12 of the return values?"
The simplest and most efficient solution by far is to just use one vector:
function vec = ols2(..)
vec = [1,2,..];
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!