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 Comments

What do you mean with moving the variable? Do you mean just copying the variable? In that case, just do s=(x+y);
I tried that as well and all I get in the workplace is ans = (x+y). I need the variable to replace the answer of the function file. Hopefully that clears up my question.

Sign in to comment.

 Accepted Answer

The below is what you are asking for, but it is NOT RECOMMENDED. If you think you need this, you have very likely analyzed the situation incorrectly!
function twosum( x,y );
%This function adds two numbers%
a=(x+y);
assignin('caller', 's', a)
end

7 Comments

Thank you soo much. That is exactly what I was looking for. I think I just have to read about assignin command. Thanks Walter.
@Robert Macawile: you clearly missed the part where Walter Roberson wrote that this "is NOT RECOMMENDED".
Some beginners think that it is very exciting being able to make variables magically jump between workspaces, but if you learn to code like this you are going to write slow, buggy, complex code that is hard to debug, and awful to work with:
For such a simple task you have picked one of the worst ways to write MATLAB code. Good luck!
@Stephen Cobeldick: Understand thanks for the clarification. Wasn't necessarily writing a program it was a question for my matlab class. Still in the beginning stages of learning matlab so will definitely keep this in mind when I do write my own. Again thanks again guys.
@Robert Macawile: the howework task specifically required that you should NOT define an output argument, or NOT allocate its output to a variable?
Why do they teach such bad programming?
I would be interested to read the complete question.
@Stephen Cobeldick: I had finally had class with my professor here was the question "1.Modify the twosum function so that it returns the result in a variable s. Suppress the printing of the result inside twosum. Copy and paste the function here." According to the question my interpretation was that ensure the ans goes back to the variable but according to her she just wanted you to type in the command window
s=twosum(x,y)
Which I thought was dumb.
Which is what all of us have been saying. It's not dumb. She's just trying to show you how to create a function that accepts values from a calling routine, and return a value computed inside that function back to the calling routine. Basic? Yes. But not dumb, since it's an essential skill that MATLAB programmers must learn and there are probably people in your class who don't even know how to do that simple thing.
@Image Analyst: Definitely over thought the question. My apologizes about that. Didn't think the answer was that simple.

Sign in to comment.

More Answers (2)

Do you mean call the function and put the result into a variable named s? Like this:
s = twosum(4,5);

8 Comments

Yes, but if I put that I get an ans and the variable s is still undefined on my workplace. Any idea how I can hide the ans and put the answer into the variable s like that code says? I know if I type that into the command window s will equal the result, but when I type twosum (4,5) I just get the ans.
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);
??
Yes. as the current function is written it outputs the ans, but the variable s is untouched. Anytime I try to check if the ans was moved to the variable is just has s in this case as undefined. Its driving me nuts.
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?
Sorry about my vagueness here's my function
function [s] = twosum( x,y );
%This function adds two numbers%
s=(x+y);
end
I type twosum (x,y) in my command window then the result is popped out as ans = (whatever x+y) is. I want to suppress the ans from showing in the command window and have variable s set to the ans instead, but every time i look at my workplace window all I see is ans = (whatever x+y) is and s is still undefined. Hopefully this clears it up.
@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)
@Stephen Cobeldick: I call the function by twosum(x,y) in the command window. As soon as I plug in an x and y the command window shows the ans but I do need the ans to be moved to the s variable.
Well, you shouldn't do that. You should instead type
s = twosym(x,y);
in the command window.

Sign in to comment.

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.

Community Treasure Hunt

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

Start Hunting!