Why do my functions suddenly return values in ASCII code?

조회 수: 1 (최근 30일)
Winter Vee
Winter Vee 2022년 6월 21일
댓글: Steven Lord 2022년 6월 21일
I have made simple functions in MatLab. For example:
function add(a,b)
fprintf('%d + %d = %d\n',a,b,a+b);
end
This used to work. However, using MatLab again after a while, this same function now returns values in ASCII code. Basically, given the inputs "4" and "5",
Before:
4 + 5 = 9
Now:
52 + 53 = 105
I don't remember doing anything that would cause this change. Simply typing "4 + 5" in the command window returns 9, but the problem arises when I make functions. Advice would be appreciated. (Version: R2022a)
  댓글 수: 4
Winter Vee
Winter Vee 2022년 6월 21일
It works, but the output seems to be in ASCII code instead of the original numbers I put it. i.e. if I input "4" and "5", it displays the input as "52" and "53". I know it "means" the same thing, but I just want to fix the formatting if that makes sense.
Stephen23
Stephen23 2022년 6월 21일
"but I just want to fix the formatting if that makes sense."
Changing the formatting cannot "fix" the fact that you call the function with incorrect inputs.

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 21일
You accidentally used "command function equivalence". When you name a function without using () then the arguments you input are passed to the function as character vectors.
add 4 5
52 + 53 = 105
add(4, 5)
4 + 5 = 9
function add(a,b)
fprintf('%d + %d = %d\n',a,b,a+b);
end
  댓글 수: 2
Winter Vee
Winter Vee 2022년 6월 21일
oh okay! thank you so much!
Steven Lord
Steven Lord 2022년 6월 21일
This also commonly happens in a standalone application. See the "Using a MATLAB File You Plan to Deploy" section on this documentation page.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by