Hi i want to display two strings in one function but G is showing but M is not i dont know what the problem is kindly help me??
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
function [G,M]=myGreeting(name, major, year)
frmtspc1='Hi, my name is %s and my major is %s.\nI started at Cal in %d.';
frmtspc='This entire char array has length %d.\n';
G=sprintf(frmtspc1,name,major,year);
D=size(G,2);
M=sprintf(frmtspc,D);
end
0 件のコメント
回答 (1 件)
Walter Roberson
2018 年 2 月 1 日
In order to see M, you need to assign the output of myGreeting to two variables, such as
[A, B] = myGreeting('Inigo Montoya', 'sword fighting', 1964);
disp(A)
disp(B)
5 件のコメント
Arslan Ahmad
2018 年 2 月 1 日
Arslan Ahmad
2018 年 2 月 1 日
編集済み: Arslan Ahmad
2018 年 2 月 1 日
Guillaume
2018 年 2 月 1 日
If you don't specifically ask for inputs matlab only returns the first one. So as Walter said, if you want to see the two outputs, you have to ask for two outputs by giving two output variables when you call the function:
>>[A, B] = myGreeting('Inigo Montoya', 'sword fighting', 1964)
A =
Hi, my name is Inigo Montoya and my major is sword fighting
I started at Cal in 1964.
B =
This entire char array has length 49.
Arslan Ahmad
2018 年 2 月 1 日
Steven Lord
2018 年 2 月 1 日
When you call a function in MATLAB without an output, most of the time MATLAB assigns the first output from that function to a special variable named ans.
Call your function with one or more output arguments and MATLAB will assign the appropriate number of outputs that your function is declared to return into those output arguments. For example, in Guillaume's comment myGreeting is called with two output arguments, A and B. MATLAB will take the first output argument myGreeting says it will return (the variable G) and assign it into the variable named A, and similarly for the variables M and B.
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!