Why is the variable's type undefined?

2 views (last 30 days)
jun
jun on 14 Oct 2022
Edited: Dyuman Joshi on 14 Oct 2022
I want a function whose input recognizes the input as its string form and the output is the inverse of it.
but It does not recognize the input as a string format. What is the problem?
p.s. I am using matlab octape version.
Below is the function I set and the result displayed in Octave.
>> function y = WWW(x)
A='x';
result = fliplr(A);
end
>> WWW(sadf)
error: 'sadf' undefined near line 1, column 5
  3 Comments
jun
jun on 14 Oct 2022
Is there any way to make the unquoted input value be recognized as a string in the function?
Dyuman Joshi
Dyuman Joshi on 14 Oct 2022
Edited: Dyuman Joshi on 14 Oct 2022
You have to input a string for the function to work the way you want it to.
The way you have defined your function it will only flip the char 'x', regardless of any string input and your output variable is not assigned correctly.
I am not sure why you have to make a function for a function that already exists.
You can directly do this -
f=fliplr(input_string)
Edit
"Is there any way to make the unquoted input value be recognized as a string in the function?"
In context of the above funciton, No (unless your input is strictly numeric)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Oct 2022
Is there any way to make the unquoted input value be recognized as a string in the function?
Only in the case where what is being passed in to the function is an unindexed variable name, and you (for whatever reason) what to manipulate the name of the variable. In such a case you could use inputname . Note that inputname() is empty for any kind of expression. And note that this absolutely cannot work for undefined names.
In MATLAB, all arguments to a function are fully evaluated before the function itself is called, so if (hypothetically) MATLAB were to have a function parameter mark to say that a parameter position was not to be evaluated, then MATLAB would have to change the entire execution model.
There is only one syntax in MATLAB to indicate that an expression might not be fully evaluated: the && and || operators are "short-circuit" operators that sometimes only need to execute the first operand. However, these are true syntactic operations, not functions. If you look at the table at https://www.mathworks.com/help/matlab/matlab_oop/implementing-operators-for-your-class.html you will see that the other operators can all be overloaded, but not these two.
There is also an argument to be made that the Symbolic Toolbox piecewise operator does not fully evaluate the operands, but technically it does evaluate all of the parameters before calling the function... it is just that the function might cause selective behaviour as a result of its operation.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!