How to write a function to display two messages to the user (in Matbal 2011)?

Hello everyone. I've been reading a tutorial about Matlab 6.1 but I'm working with Matlab 2011.
(I'm sorry if I make a mistake talking about string/integer but I'm new to Matlab and I use to program in C/Java/...)
I have to write a simple function doing the following:
- it takes two string (name1 and name2)
- it returns two integer representing how many characters the two string have (size1 , size2)
- the function print two lines:
  • "Hello name1" (where name1 is what I pass to the function)
  • "Hello name2" ((where name2 is what I pass to the function)
The code (it works in Matlab 6.1) provided by the tutorial is:
function [size1,size2] = helloNames (name1,name2)
% print Hello + name 1 , Hello + name 2
[rowNumber,size1] = size(name1);
[rowNumber,size2] = size(name2);
disp(cat(2,'Hello ',name1));
disp(cat(2,'Hello ',name2));
But in Matbal 2011 it doesn't works and I've tried to modify it but I can't get the same result.
Any suggestion? And what's used for "cat"?
Thank you in advance

回答 (3 件)

Sean de Wolski
Sean de Wolski 2012 年 2 月 24 日
doc cat
for more info on cats. That looks like it should work, what error are you recieving?
More per comments
A = rand(10,15);
%example1
[junk, sizeEX1] = size(A);
%example2
[~, sizeEX2] = size(A);
%example3
[sizeEX3] = size(A);
Compare the various variables.
Manuel
Manuel 2012 年 2 月 24 日
@Srinivas.. well its' strange It works now (but I'm sure it didn't worked). Just a question: function [size1,size2] = helloNames (name1,name2) % print Hello + name 1 , Hello + name 2 [rowNumber,size1] = size(name1); [rowNumber,size2] = size(name2); disp(cat(2,'Hello ',name1)); disp(cat(2,'Hello ',name2));
give the warning "rowNumber appears to not be used"(and I understand this)..If I click fix rowNumber is replaced witch "~". What does "~" means? And what's difference there is between:
[~,size1] = size(name1);
[~,size2] = size(name2);
and
[size1] = size(name1);
[size2] = size(name2);
?

3 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 2 月 24 日
[~,size1] = doesn't use memory filling that output with stuff you plan to use but makes size1 the second output argument
[size1] = makes size1 the first output argument.
Manuel
Manuel 2012 年 2 月 24 日
Thank you for your answer. But I can't understand the explanation.
Could you make an example please?
Sean de Wolski
Sean de Wolski 2012 年 2 月 24 日
see edit in my answer

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2012 年 2 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by