splitting a char with no delimiter

Hi,
Does anyone know how to split a four character cell array string into 2(or better 4) separate strings? A= 'A5C9' %splitting into A1=A5 A2=C9
or even better something like A(1)=A A(2)= 5 A(3)= C A(4)= 9 so that I could use them to index a larger array?
For clarity this question was incorrectly posed, the problem was having a char within a cell, and needing to extract the char and index it. My bad for not realising the differences between strings and chars and should not have written {'...'}, much thanks to all who helped clarify.
Best regards
Steve

8 Comments

A={'A5C9'};A=A{1};
This is already working like you describe, although the elements are all still chars, not normal values. Also, how were you planning on using letters to index things? Would you convert them with hex2dec?
Note:
A = 'A5C9' %no enclosing {}
could be called a string. Better would be to call it a char array as since R2016b there is also a string type.
A = {something}
is a cell array of something. Not the same thing at all.
A{1} = A5
is not even valid matlab syntax.
I point that out because your sloppy notation makes it very hard to understand what it is exactly you want. For example, the last part of your question does not make much sense. if
A = 'A5C9'
then A(1) is already 'A', A(2) is already '5', etc.:
>> A = 'A5C9';
>> A(1)
ans =
'A'
>> A(2)
ans =
'5'
Hi,
I would remove the letter to just leave the number. I have a method using regexp but wondered if there was a more elegant way.
expression2='A\d';
modNameOut = regexp(input,expression2,'match')
when I look in my code A(1) gives me 'A2C9', not the individual 'A', I'm not sure why but did see this being common in other similarly posed questions.
Stephen23
Stephen23 on 15 Jan 2018
Edited: Stephen23 on 15 Jan 2018
" I'm not sure why but did see this being common in other similarly posed questions."
Because for some unexplained reason you have stuck that char vector into a cell array. What is the point of the superfluous cell array?
i wouldn't say it is superfluous, I get the a2C9 string by taking that string out of a filename, it refers to a batch number. So once I know what batch2 lot 9 is I can place the data for that test into a larger table and 3D plot it.
I am not sure why my cell, which tells me it is a string, does not have the A(4)=9 but it doesn't, maybe because i am looping through 8 data files and extracting this, I get
>> A(2)
ans =
cell
'A1C3_'
Stephen23
Stephen23 on 15 Jan 2018
Edited: Stephen23 on 15 Jan 2018
"I am not sure why my cell, which tells me it is a string"
Because cell arrays can contain any other data types it actually tells us nothing about the class of its contents.
"I am not sure why my cell, which tells me it is a string, does not have the A(4)=9"
Because what you appear to have is a char vector in a cell array. And clearly it makes no sense to try to access the individual characters while it is still in the cell (so you need to use some cell indexing first). But this is all speculation, because so far you have told/shown us about four different explanations of what you have (title says char, text says string, example shows cell with char). It might be easiest for everyone if you simply uploaded the complete variable in a .mat file for us to look at.
Yes Stephen, that is what I was not aware of until you pointed it out, I now know what I was doing wrong. Very much appreciate your help with this.

Sign in to comment.

 Accepted Answer

when I look in my code A(1) gives me 'A2C9', not the individual 'A
As I pointed out
A = {'A2C9'}
is not the same thing as
A = 'A2C9'
The former is a cell array. A container, which in this case has only one element, a char array. If your cell array only ever has one element, then you don't need it.
Note that if
A = {'A2C9'}
then A(1) is still {'A2C9'}, exactly the same cell array. Older versions of matlab did not display the enclosing {}, a source of confusion. Newer versions of matlab no longer do this. To get the content of the cell array, you use {} to index it:
>> A = {'A2C9'}
A =
1×1 cell array
{'A2C9'}
>> A{1}
ans =
'A2C9'
>> A{1}(1)
ans =
'A'

5 Comments

Sorry, I have A = 'A2C9'
bit confusing as my workspace told me it was Class string, I still get confused between char and str. Apologies.
'A2C9' % a 1x4 char array
"A2C9" % a scalar string
You can use indexing to access individual characters of the char array.
From the other comments, it would appear you have A as
>>A = {'A2C9', 'A1C3_', 'and maybe more'}
A =
1×3 cell array
{'A2C9'} {'A1C3_'} {'and maybe more'}
This is a cell array of char arrays, which you could call a cell array of strings since it appears you're on an older version of matlab.
You can access the individual strings using {} indexing. This returns the content of the cell you index:
>> A{1}
ans = 'A2C9'
>> A{2}
ans = 'A1C3_'
The use of {} is very important. If you use A(1) instead you get a subset of the cell array. It is still a cell array. In newer versions of matlab this is made clear:
>> A(1)
ans =
1×1 cell array
{'A2C9'}
Once you've indexed into a cell using {}, you have a char array that you can index normally using ():
>> A{1}(1)
ans = 'A'
>>A{1}(2)
ans = '2'
>>A{2}(5)
ans = '_'
Yes, this is the point I was not aware of, I thought chars and strings were almost synonyms, I didn't;t appreciate I could extract the char and then index it. If you put this as an answer I will accept this.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!