Why does Matlab give me the entire string instead of just one element?

Hi, I'm trying to extract a value of a string, but if I type string(1) Matlab gives me the entire string.. How should I solve this?

回答 (3 件)

YT
YT 2018 年 9 月 6 日

1 投票

It would be usefull if you read the documentation on strings (and maybe also char)
mystring = "HelloWorld"; %notice double quotes
mychar = 'HelloWorld'; %notice single quotes
mystring(1)
%Output: HelloWorld
mychar(1)
%Output: H
string2char = char(mystring);
string2char(1)
%Output: H
As you can see you can easily make a char array from a string like this

4 件のコメント

AelinAG
AelinAG 2018 年 9 月 6 日
Thanks for answering! Unfortunately I'm not allowed to use string2char(it is for a school assignment).
YT
YT 2018 年 9 月 6 日
string2char is just a (unfortunate) variable name to store the result of making a character array from your string (and not a build-in function). You could name it whatever, doesn't really matter.
AelinAG
AelinAG 2018 年 9 月 6 日
Ah I understand, sorry, I'm new to programming! My string is a number that is given in the input of a function though, so how do I extract one of the elements of that number?
YT
YT 2018 年 9 月 6 日
Something like this then
input = "987654"; %input string
temp = char(input); %temporary variabele
output = temp(1) %will output '9' of type char

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

ADragon
ADragon 2018 年 9 月 6 日

0 投票

I am guessing you might have a cell array instead of a string. What happens when you do this?
str = 'Hello Matlab';
str(1)
It should return 'H'. If you have a cell array it will return the whole string. For example:
c = {'Hello Matlab'};
c(1)
To get the first character use
c{1}(1)
which is first cell, first element of cell array c.
AD

5 件のコメント

AelinAG
AelinAG 2018 年 9 月 6 日
Thanks for answering my question! Unfortunately, it still doesn't work :( Matlab returns: Brace indexing is not supported for variables of this type. My string is a number, maybe it has something to do with that?
ADragon
ADragon 2018 年 9 月 6 日
Can you post some code? You could possibly use num2str.
AelinAG
AelinAG 2018 年 9 月 6 日
That's not allowed, it's for an assignment and we can't use num2str or str2num :(
ADragon
ADragon 2018 年 9 月 6 日
That stinks. Your teacher must be after something very specific. I would check docs for the functions that you CAN use. I'm not sure how to do what you are asking without functions.
Walter Roberson
Walter Roberson 2018 年 9 月 7 日
Brace indexing is not supported for variables of this type
That establishes that your input is not a cell array of character vectors, and also establishes that your input is not a string object.
If your input were a character vector then using (1) to index it would have only returned a single character instead of the whole thing.
I therefore suspect that you are not being given a cell array or a character vector or a string object. I suspect that you are being passed a numeric object. What shows up if you ask for
class(NameOfVariableGoesHere)
?

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

Image Analyst
Image Analyst 2018 年 9 月 7 日

0 投票

It's probably because string is a built-in function name. Use a different name for your variable. From the help for the string() function:
Description
example
str = string(A) converts the input array to a string array.
For example:
>> string(1)
ans =
"1"

カテゴリ

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

質問済み:

2018 年 9 月 6 日

コメント済み:

2018 年 9 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by