Cell array strings storage

2 views (last 30 days)
Micha
Micha on 25 Jul 2018
Commented: Micha on 25 Jul 2018
Two questions regarding cell arrays and strings:
1. When I enter a cell array that contains strings through the workspace, sometimes the strings appear in the cells as 'string' (with an apostrophe) and other times the strings appear as they are, without the apostrophe. I couldn't find a rule what happens when, or what is the differenece between these cases.
Is there an explanation for this?
2. When I initialize an array of strings in such a way that the strings appear in the workspace cell array with an apostrophe, and then try to access an element using array{1}, I get:
ans =
cell
'a'
to prevent the "cell" from appearing, I need to double access it (as if it is a cell inside a cell):
array{1}{1}
How can I initialize this in such a way that I can access that string directly without the double {}{}?
Thanks
  3 Comments
Guillaume
Guillaume on 25 Jul 2018
Micha comment mistakenly posted as an answer moved here:
Yes you are right, every occurrence of the word "string" in my original post was supposed to be "char array".
Anyway, here is some code:
array = {'a', 'b', 'c'};
here, array{1} == 'a'
but then in this case: array2 = cellfun(@(x) regexp(x, '.*', 'match'), array)
you have to do array2{1}{1} to get that same 'a'

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 25 Jul 2018
you have to do array2{1}{1} to get that same 'a'
Yes, of course, because in the second case, you have a cell array of cell arrays of char array, not a cell array of char arrays. You get a cell array of cell arrayw because regexp can return multiple match for each cell of your orignal array. If you want to limit it to just one match (the first one if there are more than one possible), then tell regexp.
Note that your cellfun was completely unnecessary. regexp works directly with cell arrays, so instead of:
array = regexp(array, '.*', 'match'); %cellfun unnecessary
use
array = regexp(array, '.*', 'match', 'once'); %force only one output from regexp,
  5 Comments
Micha
Micha on 25 Jul 2018
Good stuff. Thanks.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 25 Jul 2018
Edited: Stephen23 on 25 Jul 2018
With older MATLAB versions the rule was very simple: all arrays were displayed without the outermost array brackets/parentheses (and only showing the contents for the first nesting and only scalar numeric inside cells):
Char array:
>> 'abc' % -> remove ''
ans =
abc
>> {'abc'} % -> remove {}
ans =
'abc'
>> {{'abc'}} % -> remove {}
ans =
{1x1 cell}
Scalar numeric:
>> 1 % -> remove [] (implicit)
ans =
1
>> {1} % -> remove {}
ans =
[1]
>> {{1}} % -> remove {}
ans =
{1x1 cell}
Numeric array:
>> 1:3
ans =
1 2 3
>> {1:3}
ans =
[1x3 double]
>> {{1:3}}
ans =
{1x1 cell}
At some point recently this seems to have changed for cell arrays...

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!