Function input vector specifiers

3 次查看(过去 30 天)
Jay
Jay 2020-10-1
Is there a way to specify all elliments of a vector in a functions input term?
ie. instead of writing
val_1 = mat_val(1,1)
val_2 = mat_val(1,2)
val_3 = mat_val(1,3)
val_4 = mat_val(1,4)
function [ret_val] = filename (val_1, val_2, val_3 , val_4)
ret_val = val_1*val_3 - val_4 + val_2
end
I can have a specifer with the correct syntax to match
function [ret_val] = filename(mat_val(1,:))
ret_val = val_1*val_3 - val_4 + val_2
end
The functions return will be called in a seperate script file.
Thanks

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-10-1
It is always a good idea to use an array instead of separate variable names: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. However, the following shows a somewhat acceptable way
function [ret_val] = filename(varargin)
[val_1, val_2, val_3, val_4] = varargin{:};
ret_val = val_1*val_3 - val_4 + val_2;
end
Call it like this
y = filename(1, 2, 3, 4)

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by