How would I suppress function answer and move the answer to a variable all within the function? Homework
이전 댓글 표시
Hello guys new to Matlab and have probably a dumb question, but I am trying to suppress the ans from displaying, and would like the answer to be moved to a variable.
function [a] = twosum( x,y );
%This function adds two numbers%
a=(x+y);
end
I need to move the answer to a variable s. Any help would be great thanks.
댓글 수: 2
Rik
2017년 9월 13일
What do you mean with moving the variable? Do you mean just copying the variable? In that case, just do s=(x+y);
Robert Macawile
2017년 9월 13일
채택된 답변
추가 답변 (2개)
James Tursa
2017년 9월 13일
Do you mean call the function and put the result into a variable named s? Like this:
s = twosum(4,5);
댓글 수: 8
Robert Macawile
2017년 9월 13일
Walter Roberson
2017년 9월 13일
Do I understand correctly you want to be able to type in exactly
twosum(4,5)
at the command line and have it output no visible result but that it should update the variable s anyhow? Without you having to type
s = twosum(4,5);
??
Robert Macawile
2017년 9월 13일
I do not get ans at all, and s is correctly assigned the value:
>> s = twosum(4,5)
s =
9
>>
Whatever you are doing sounds very strange. Why not show us exactly how you are calling your function?
Robert Macawile
2017년 9월 13일
Stephen23
2017년 9월 13일
@Robert Macawile: I will repeat my request one more time, in case you missed it: Why not show us exactly how you are calling your function?.
I did not ask for you to show us your function again.
It sounds like you are calling the function without allocating the output to any variable (e.g. s). Is this correct? Why not simply allocate the output to s (which seems to be what you are trying to do) ?
Are you clicking the green Run button in the toolbar? (If you are: DON'T)
Robert Macawile
2017년 9월 13일
Walter Roberson
2017년 9월 13일
Well, you shouldn't do that. You should instead type
s = twosym(x,y);
in the command window.
Image Analyst
2017년 9월 13일
When you "type twosum (x,y) in my command window" add a semicolon after the command to suppress the output.
So do it like this:
result = twosum (x,y); % Semicolon will suppress echo of the result to the command window.
NOT like this:
result = twosum (x,y) % No semicolon will echo result to the command window.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!