Filter löschen
Filter löschen

multiple function outputs

26 Ansichten (letzte 30 Tage)
David Craig
David Craig am 20 Aug. 2011
Kommentiert: Adithya Papa am 2 Feb. 2020
hi i am working through an online tutorial and having trouble with the following function,
function [summ,prodd] = sumprod(x1,x2)
% usage:
% [summ, prodd] = sumprod(x1,x2)
% x1 = a complex number
% x2 = another complex number
% summ = sum of x1 and x2
% prodd = product pf x1 and x2
prodd = (x1)*(x2);
summ = x1 + x2;
I want it to output the two variables but it only gives me the sum. thanks,
David
  4 Kommentare
Abinash Mandal
Abinash Mandal am 14 Jun. 2019
Suppose we only want second output (product in this case), then how to do that??
Stephen23
Stephen23 am 15 Jun. 2019
Bearbeitet: Stephen23 am 15 Jun. 2019
@Abinash Mandal: by reading this comment below, from three years ago, and the comment that follows it:
Or by using your favorite internet search engine to search for "MATLAB ignore function outputs" and reading the first link that is returned:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 20 Aug. 2011
call it this way: [s,p]=sumprod(x1,x2)
  2 Kommentare
Kannapiran Arasu
Kannapiran Arasu am 8 Sep. 2016
For this I always gt the first value namely s. If I want only p how to go about it?
Fangjun Jiang
Fangjun Jiang am 8 Sep. 2016
You can do this. [~,p]=sumprod(x1,x2). Please note that earlier version of MATLAB does not support this "~" syntax.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

amatlabr
amatlabr am 22 Mär. 2018
Bearbeitet: amatlabr am 22 Mär. 2018
I know is an answered question, but I wrote two simple functions in different m-file under same folder :
function [ somma,prodotto ] = sommaprodotto( a,b )
somma = a +b;
prodotto = a*b;
end
and a main() calling the above function :
function [s,p]=main( )
val1 = 39;
val2 = 21;
[s,p]= sommaprodotto(val1,val2);
end
I click on run button but I only get the first value, why? I also noticed if I write calling function without semicolon ( [s,p]= sommaprodotto(val1,val2) ) I get both values in the command window, but not in the working space, why? Thanks
  1 Kommentar
Stephen23
Stephen23 am 22 Mär. 2018
Bearbeitet: Stephen23 am 22 Mär. 2018
"I click on run command but I only get the first value"
Because you click on the "Run" button. Do NOT use that button. That button is the worst design decision in the history of MATLAB, because no beginner knows what it really does. Ignore that button.
To run any code simply type it in the command window (or another script/function/...) together with any required input and output arguments and then press enter. For example:
>> [S_out,P_out] = main()
and then press enter. You will get both outputs.
"I also noticed if I write calling function without semicolon ... I get both values in the command window, but not in the working space"
Which workspace? Where are you calling this function? How are you checking if the variables are in that workspace? Try writing whos straight after when you call that function: are those variables listed?

Melden Sie sich an, um zu kommentieren.


Adithya Papa
Adithya Papa am 2 Feb. 2020
function [summ,prodd] = sumprod(x1,x2)
prodd = (x1)*(x2)
summ = x1 + x2;
  1 Kommentar
Adithya Papa
Adithya Papa am 2 Feb. 2020
Now it will give 2 outputs.The semicolon after the prodd suppresses the output.That's why it is not showing

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by