defining a function output properly

1 回表示 (過去 30 日間)
Riccardo Tronconi
Riccardo Tronconi 2021 年 6 月 17 日
編集済み: Stephen23 2021 年 6 月 17 日
Hi guys! I have a problem in properly defining my output in a function.
According to n= max(position{:,4}); I have different T1, T2,..Tn as arrays.
But how to define all of these in my function output if I do not know in advance how many of them I will have?
[T,n] = paramdisp(position)
Doing so it does not work.
Thanks. I'm new to matlab.

採用された回答

Steven Lord
Steven Lord 2021 年 6 月 17 日
So you want to call a function that can return different numbers of outputs and you want to get a number of outputs that is only known when the code is run not when it is written?
Can you define variables with numbered names like X1, X2, X3, ... ? Yes.
Instead I recommend building a cell array and using a comma-separated list.
% Sample data
A = magic(5);
% Preallocate the cell to hold the desired number of outputs
numOutputs = 2;
theOutputs = cell(1, numOutputs);
% Fill in the cell
[theOutputs{:}] = svd(A);
% Call the function with the same number of outputs specified explicitly
% when the code is written so we can compare to the comma-separed list
[output1, output2] = svd(A);
% Compare the two approaches
isequal(theOutputs{1}, output1)
ans = logical
1
isequal(theOutputs{2}, output2)
ans = logical
1
If you wanted to call svd with three outputs using the theOutputs cell array the only thing you'd need to change is the line that defines the numOutputs variable.
  2 件のコメント
Riccardo Tronconi
Riccardo Tronconi 2021 年 6 月 17 日
Thanks for your comment! Just to try, is it possible to use the same approach with arrays?
Stephen23
Stephen23 2021 年 6 月 17 日
編集済み: Stephen23 2021 年 6 月 17 日
"...is it possible to use the same approach with arrays?"
A cell array already contains arrays. Every array in a cell of a cell array is itself an array.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by