How would I suppress function answer and move the answer to a variable all within the function? Homework
Mostrar comentarios más antiguos
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 comentarios
Rik
el 13 de Sept. de 2017
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
el 13 de Sept. de 2017
Respuesta aceptada
Más respuestas (2)
James Tursa
el 13 de Sept. de 2017
Do you mean call the function and put the result into a variable named s? Like this:
s = twosum(4,5);
8 comentarios
Robert Macawile
el 13 de Sept. de 2017
Walter Roberson
el 13 de Sept. de 2017
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
el 13 de Sept. de 2017
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
el 13 de Sept. de 2017
Stephen23
el 13 de Sept. de 2017
@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
el 13 de Sept. de 2017
Walter Roberson
el 13 de Sept. de 2017
Well, you shouldn't do that. You should instead type
s = twosym(x,y);
in the command window.
Image Analyst
el 13 de Sept. de 2017
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.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!