Creating a function to return a string into upper and lower case

I am trying to create a function where U returns all the characters in the string in uppercase and where L returns all the characters in the string to lowercase. I have created a function however, when I test it in the command window I get no information. Not even an error in my code.
function uppercase_lowercase(x)
U=lower(x);
L=upper(x);
end

 採用された回答

Rik
Rik 2019 年 10 月 29 日

1 投票

Matlab did exactly what you asked: you wrote a function with an input, but without an output. I you open this in the Matlab editor, the m-lint will give you a warning, explaining that it looks like you aren't doing anything with the variables U and L. If you want them as outputs, make sure to included that in your function header:
function [U,L]=uppercase_lowercase(x)
U=lower(x);
L=upper(x);
end
Your function is also missing a header line, documentation (including usage examples), comments, and input checking. For such a tiny wrapper function the header line would probably do.

4 件のコメント

Britnie Casillas
Britnie Casillas 2019 年 10 月 29 日
I added [U,L] as the output but now it is only returning lowercase.
How do I write it so the answer gives me both?
>>uppercase_lowercase(x)
ans =
'now is the time for all good'
Steven Lord
Steven Lord 2019 年 10 月 29 日
While the parts of your function Rik mentioned (header line, documentation, comments, input checking) aren't required it's good to get in the habit of adding them. If you're not sure how to add a header line or documentation see this documentation page for more information. The first related topic on that page discusses adding comments to your code.
I also agree with Rik that simply adding the header line seems sufficient for this short function.
Rik
Rik 2019 年 10 月 29 日
@Britnie: The same way you do it for every other function with multiple outputs, write the line below in you command window:
[A,B]=uppercase_lowercase('now is tHe Time for aLL gOod');
@Steven: thank you for adding the doc link. I tend to avoid emphasizing that those parts are optional. But maybe in this case I was a bit too zealous when I described them as 'missing'.
Britnie Casillas
Britnie Casillas 2019 年 10 月 29 日
Thank you. I got it to work.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by