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 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!