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
Adam
Adam on 25 Jul 2018
You would need to show the code you are using to produce this. Cell arrays contain exactly what you tell them to, no more nor less, so it is down to how you define the code that will determine whether you get cells inside cells or chars. I'm not sure what you mean by strings appearing 'as they are' though. From your context I assume you use string to refer to a char array rather than an actual Matlab string, but chars in a cell array will naturally have the quotes e.g.
>> A = { 'a' }
A =
1×1 cell array
{'a'}
>> A{1}
ans =
'a'
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
Guillaume
Guillaume on 25 Jul 2018
A match can contain any number of tokens (what everybody else calls capturing group). With some regex engines the number of captures returned may vary per match (if the capture is optional and didn't match anything, it may be excluded). In matlab, an optional capture is still returned (as empty) so the count is fixed per match. Since the number of captures can vary matlab wraps them into a cell array, even if your particular regular expression only has one capture. There is no way around that.
However, if you have only one capture, you can usually rewrite the regex into a match by using look-ahead and look-behind.
Or, if you use the 'once' option to force only one match per input, you could just vertcat the outer cell arrays:
array = {'an apple', 'a pear', 'a grenade'};
array2 = regexp(array, 'a(.)([^aeiou ]*)', 'tokens', 'once'); %a 1x3 cell array of 1x2 cell arrays
array2 = vertcat(array2{:}) % a 3x2 cell array, only works if 'once' was specified.
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 Characters and Strings 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!