I am writing a function with two arguments and I want the second argument to be processed as a string. The following code encounters an error
function Derivative = derive ( Matrix9x1 , string Variable )
end
How can I tell it to matlab?
I mean even if the user inputs 1 as the second argument it should be processed as an string
and the user should be able to enter for example omega
The second argument to your function will only be processed ‘as a string’ if it ‘is a string’, that is if you enclose it in single quotation marks. If you want to pass a number to a function and turn it into a string for further operations, use the function
num2str. If you want to write a function which takes different actions depending on the type of the second argument you’re going to have to test that type when the function is called; you might want to look at the functionsischar,isstrprop,isnumeric, and their relations.Oh, and don’t forget that a Matlab ‘string’ is really an array of characters which are just a convenience ‘type’ for integers-representing-characters.