How can I transmit multiple variables to a function in the form of a single array?
3 ビュー (過去 30 日間)
古いコメントを表示
My function has 20 inputs, in other words, it looks something like this:
function [output1,output2]=multi_input(a,b,c,d,e,f,g,h,i,j,k)
...
end
Now of course, the obvious answer would be, to me, rewrite the function as accepting a single input. But let's say I don't want to do this. How can I, in an effort to have a better readable code that doesn't go over the margin, tell multi_input.m to treat an array A that is a 1x20 to accept each array element as an input to the function? In other words, using the function like this:
A(1)=...;
A(2)=...;
...
A(20)=...;
[output1,output2]=multi_input(A)
0 件のコメント
採用された回答
Jan
2021 年 3 月 7 日
編集済み: Jan
2021 年 3 月 7 日
You can use a struct:
S.a = a;
S.b = b;
...
[output1,output2] = multi_input(S)
function [output1,output2] = multi_input(S)
"code that doesn't go over the margin" - you can use the line continuation:
function [output1, output2] = multi_input( ...
a, b, c, d, e, ...
f, g, h, i, j, k)
"tell multi_input.m to treat an array A that is a 1x20 to accept each array element as an input" - this is no valid Matlab syntax and there is no magic trick to expand the Matlab syntax to your needs. The nature of programming is to express the problem in the existing language, not to adjust the language to the fit the problem.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!