Why is the variable's type undefined?

조회 수: 2 (최근 30일)
jun
jun 2022년 10월 14일
편집: Dyuman Joshi 2022년 10월 14일
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
jun
jun 2022년 10월 14일
Is there any way to make the unquoted input value be recognized as a string in the function?
Dyuman Joshi
Dyuman Joshi 2022년 10월 14일
편집: Dyuman Joshi 2022년 10월 14일
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)

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 10월 14일
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.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by