Use input from an array in a function

2 ビュー (過去 30 日間)
LR
LR 2021 年 5 月 13 日
コメント済み: Star Strider 2021 年 5 月 13 日
I have data stored in an array called 'plan' i want to use as an input in a function. I'm calling using this: Area=CalculateArea(w,l);
The function is this:
function [result] = CalculateArea( w,l )
w=plan(j,1,i);
l=plan(j,2,i);
result=(w*l);
end
Please advise why this does not work. Any help would be appreciated.
Thank you

採用された回答

Star Strider
Star Strider 2021 年 5 月 13 日
The ‘plan’ array is not being passed to your function, and since the function has its own workspace (that it does not share with the calling script workspace), ‘plan’ does not exist for it.
If you are passing ‘w’ and ‘l’ to your function, and not ‘plan’ either this option (that passes only the variables, not the array) —
w=plan(j,1,i);
l=plan(j,2,i);
function [result] = CalculateArea( w,l )
result=(w*l);
end
or this option (that passes the array) —
function [result] = CalculateArea( plan, i, j)
w=plan(j,1,i);
l=plan(j,2,i);
result=(w*l);
end
would likely work.
I cannot test this, so it will likely be necessary to experiment to determine the option that works best in your application.
  2 件のコメント
LR
LR 2021 年 5 月 13 日
Thank you, the 2nd option worked, it worked this way too:
function [result] = CalculateArea( plan, i, j)
result=plan(j,1,i)*plan(j,2,i);
end
Star Strider
Star Strider 2021 年 5 月 13 日
As always, my pleasure!

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by