splitting a char with no delimiter
42 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2018-1-15
编辑:Stephen23
2018-1-15
"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.
采纳的回答
Guillaume
2018-1-15
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 个评论
Guillaume
2018-1-15
编辑:Guillaume
2018-1-15
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 = '_'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!