Reading a select number of characters from a string

How would I go about taking the first 5 characters of a string? I want to be able to take a string like "some_string" and take the first 5 characters so I get a new string "some_". I am not sure where to look in MatLab documentation for this.

4 件のコメント

Vish
Vish 2016 年 3 月 9 日
編集済み: Walter Roberson 2016 年 3 月 9 日
i want to get last 4 character from my string
i have
a=abcdefgh
b=efgh
i want b.
now there can b any no of char before last 4 character..
Walter Roberson
Walter Roberson 2016 年 3 月 9 日
b = a(end-3:end);
Shikhar Pandey
Shikhar Pandey 2016 年 7 月 13 日
for me this is not working. I am implementing it in a callback function in GUI.
Johann Alban Schöpfer
Johann Alban Schöpfer 2018 年 1 月 26 日
If you are reading this in the glorious time of the string data type, you can use the answers below by going back to a char array.
str = "I want to edit this string";
temp = char(str);
% do the things
str = string(temp);

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

回答 (5 件)

Junaid
Junaid 2011 年 12 月 25 日

7 投票

Dear Louis,
Let say you have string Variable a;
a = 'HelloMoto';
if you want to take first 5 character of this. It is straight forward.
b = a(1:5); % now b has a's first characters only.
Similarly if you have array of String like this;
A = {'String1', 'String2','String3' };
Then taking string2 as your "Some_String" and then take first 5 character would be like this.
B = A{2};
B(1:5)
I hope this is what you asked for ? Do let me know in either case.

4 件のコメント

Louis
Louis 2011 年 12 月 25 日
I'm getting an error "index exceeds matrix dimensions. This is the code:
millisecond = xlsread('someFile.xlsx', 1, entryE);
[~,message] = xlsread('someFile.xlsx', 1, entryF);
a = message(1:3);
if millisecond == 1
soundMoment = 0;
elseif strcmp( a, 'pro')
soundMoment = millisecond;
end
The code was working fine before. The only changes are adding a = message(1:3), and putting strcmp( a, 'pro') when it was strcmp( message, 'probe_sound') before.
Junaid
Junaid 2011 年 12 月 25 日
Dear Louis,
Sorry I can't execute your code. But Could you tell me your size(message). If message is array then you can't do like this.
Try this
a = message{1}(1:3)%
I assume the first string in variable message has characters more then 3. Then it should not give any error.
Louis
Louis 2011 年 12 月 25 日
Message is just read in from an Excel file. It is only a string I am reading in. I tried your suggestion, but that didn't work. Might one issue be that the first 3 characters for some of the actual message in the spreadsheet are spaces?
Kiana Maillet
Kiana Maillet 2022 年 8 月 15 日
Isn't 'that' a character array? and "this" would be a string..

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

Jurgen
Jurgen 2019 年 12 月 16 日

4 投票

If you don't want to convert to char type: extractBetween and it's brethren work on the newer string datatype.
Junaid
Junaid 2011 年 12 月 25 日

2 投票

Dear Let say your file name is "someFile.xls" then following code should work.
[m mess] = xlsread('someFile.xls', 1);
The size of mess with be number columns and rows in sheet one. Lets assume that in first row second colum there is your requires string you want to take first three initial characters. Then this code should work.
a = char(mess(1,2)); % now a contains that string
myStr = a(1:3); % this should return your first three characters.
Try and let us know, if it works. For experiments you make your own xls file so that you put the string you want. Once the code works and give desired resutls then it would be easy for you to debug your problem in your current xls file, as you mentioned due to spaces or anything.

3 件のコメント

Louis
Louis 2011 年 12 月 25 日
Yes, but does whitespace cause an issue? I tried to do strtrim, but it's still saying the index exceeds matrix dimensions.
Jan
Jan 2011 年 12 月 25 日
"mess{1,2}" is more efficient than "char(mess(1,2))".
Jan
Jan 2011 年 12 月 25 日
@Louis: The white space characters do not cause troubles, because they are characters. But if the strings have less than 3 characters, "a(1:3)" will fail.

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

Jan
Jan 2011 年 12 月 25 日

2 投票

This crops the first 3 characters, but dos not touch shorter strings:
[~,message] = xlsread('someFile.xlsx', 1, entryF);
a = message(1:min(3, length(a)));

3 件のコメント

Louis
Louis 2011 年 12 月 26 日
I figured out the issue. What reading a spreadsheet does is return a 2D array, yet message still acted as a string. Even though the data was a string, message was a 2D array.
Calum
Calum 2014 年 11 月 4 日
編集済み: Calum 2014 年 11 月 4 日
How would I keep only the first three characters from every row in a column of unknown length?
Thanks
ABDALHADI ABU ZEYNEH
ABDALHADI ABU ZEYNEH 2020 年 5 月 19 日
word= char('cacabcabac');
word(1:3)

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

Vahidreza Jahanmard
Vahidreza Jahanmard 2022 年 3 月 25 日

1 投票

If you have string format (not char format), you can use this function:
https://mathworks.com/help/matlab/ref/extract.html

カテゴリ

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

質問済み:

2011 年 12 月 25 日

コメント済み:

2022 年 8 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by