setting a variable to the input
5 views (last 30 days)
So I have this code, when I try to run it in the command window, it keeps saying too many input arguments.
I'm trying to set r= open incase I want to enter mutiple variabls.
r=[ 2 4 5] or r=[ 2 4 6 7 8]
it keeps coming back too many inputs arguments.
Robert U on 13 May 2020
Hi Jian Chen,
The functionality that you describe cannot be done. Using object oriented programming the functionality could be close. With functions the call routine is different.
The function you write shadows the built-in command roots() anyway since it uses the same name. It does not make much sense to wrap an own function around a built-in function that is using the input unaltered. But for the sake of completeness there is an example for that below as well.
Functions - Mathworks Function Basics
function p = myRoots(r)
p = roots(r);
Call it by writing to command line for r = [3 -2 -4]:
myRoots([3 -2 -4])
Object oriented programming - Mathworks Classes Basics
r; % factor representation of polynomial
function obj = myClass(r)
% MYCLASS Construct an instance of this class
if nargin == 1
obj.r = r;
elseif nargin > 1
error('Too many inputs.')
% do nothing
function obj = set.r(obj,input)
% set method for property "r", check validity of input
obj.r = input;
function p = roots(obj)
% method "roots" returns polynomial roots of property "r" using built-in function "roots"
p = roots(obj.r);
Working with the class to achieve same result as above:
myObject = myClass([3 -2 -4]);