how to return cell array with varargout?

23 ビュー (過去 30 日間)
Louis Tomczyk
Louis Tomczyk 2022 年 7 月 12 日
編集済み: Stephen23 2022 年 7 月 12 日
Dear all,
I know the title is not that explicit but I did not find a short way to formulate my question.
Here (MATHWORKS - Simple explanation - how to use varargout) was given an example to use .
I remind here the code suggested:
function varargout = varargoutexample(x)
% Demonstrates how to use VARAGOUT.
% Pass in a vector of values.
% Note that NARGOUT is the number of output
% arguments you called the function with.
for ii = 1:nargout
varargout{ii} = x.^ii;
end
How can I change the code so as to get a dynamic cell array as an output.
To be more explicit I imagine something like:
function varargout = varargoutexample(N,x)
for ii = 1:N
varargout{ii} = x.^ii;
end
such as when I call the function I have:
% in the shell
my_cell = cell(1,3); % pre-allocation
>> my_cell = varargoutexample(3,2)
my_cell =
1x3 cell array
{[2]} {[4]} {[8]}
And then get easy acces to the elements of my_cell :
my_cell{1} = 2
my_cell{2} = 4
my_cell{3} = 8
When I print the loop I have the expected result:
>> my_cell = varargoutexample(3,2)
ii =
1
varargout =
1×1 cell array
{[2]}
ii =
2
varargout =
1×2 cell array
{[2]} {[4]}
ii =
3
varargout =
1×3 cell array
{[2]} {[4]} {[8]}
But when I call my_cell I only get 2 which corresponds to the first loop result...
I hope I am clear enough.
Thanks a lot in advance!
Best regards,
louis
  3 件のコメント
Steven Lord
Steven Lord 2022 年 7 月 12 日
And the pattern Adam Danz showed in the accepted answer matches the "Function Return Values" section on the first of the pages to which Stephen23 linked.
Louis Tomczyk
Louis Tomczyk 2022 年 7 月 12 日
編集済み: Louis Tomczyk 2022 年 7 月 12 日
@Stephen23 thanks for the link, I didn't think about typing such keywords...

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

採用された回答

Adam Danz
Adam Danz 2022 年 7 月 12 日
myCell = cell(1,3);
[myCell{:}] = myfunc(__)
  1 件のコメント
Louis Tomczyk
Louis Tomczyk 2022 年 7 月 12 日
such reactivity!
perfect, thanks a lot!
Best,
louis

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

その他の回答 (1 件)

Jonas
Jonas 2022 年 7 月 12 日
if i understand you cirrectly, you want as dynamic output variable varargout, and each of the given should/can be a cell itself
so not this
[a,b,c]=varargoutexampleMultVar(3,2)
a = 2
b = 4
c = 8
but you want a cell
a=varargoutexample(3,2)
a = 1×3 cell array
{[2]} {[4]} {[8]}
function varargout = varargoutexampleMultVar(N,x)
varargout=cell(1,N);
for ii = 1:N
varargout{ii} = x.^ii;
end
end
function varargout = varargoutexample(N,x)
for ii = 1:N
varargout{1}{ii} = x.^ii;
end
end
  3 件のコメント
Stephen23
Stephen23 2022 年 7 月 12 日
Note that VARARGOUT does absolutely nothing in the VARARGOUTEXAMPLE function, it can be simply replaced with a normal named output argument:
a = normalexample(3,2)
a = 1×3 cell array
{[2]} {[4]} {[8]}
function out = normalexample(N,x)
out = cell(1,N);
for ii = 1:N
out{ii} = x.^ii;
end
end
Louis Tomczyk
Louis Tomczyk 2022 年 7 月 12 日
@Stephen23 Yes that was obvious but I still don't understand the idea of needing to put exactly the right number of output arguments, for a function which will return time-varying number of outputs....
Indeed the syntaxt :
[mycell{:}]
is not obvious to me even if I admit I am far from being an expert...

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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by