Difference between character variables where one shows the whole word versus the value of an array?

I'm using a search function (credit: https://www.mathworks.com/matlabcentral/fileexchange/41357-search--web-search-from-the-command-line?focused=3785132&tab=function) that opens up google and searches up whatever word you want.
For some reason, when I try searching using a character variable that shows the whole word (i.e char1 = 'Hello World'), it works fine. When I try searching with a character variable that shows only an array (i.e char2 = 1x20 char), nothing happens.
For context, the char2 is being pulled from the ocr command to identify words in a picture.
I'm not sure what the difference between the way the values are stored and if there's a way to fix this.
Thanks!

1 Comment

Simple:
  • char1 is a char vector.
  • char2 is a cell array, which contains a char vector.
You can check this using class, or whos.

Sign in to comment.

 Accepted Answer

'Pends on how you're storing and addressing it...a char() array is just that an array of characters and so, just like a numeric array you must explicitly address every character you want (or use the variable name without any subscripting at all which is equivalent to 1:numel as the subscripting expression; the whole thing iow.
Example:
>> c='Hello' % define a character array string
c =
'Hello'
>> whos c % what's it look like in memory???
Name Size Bytes Class Attributes
c 1x5 10 char
>> c % address the variable; get it all
c =
'Hello'
>> c(1) % use subscript--oops, only got the character asked for
ans =
'H'
>> c(1:end) % explicitly ask for all
ans =
'Hello'
>> c(:) % use the colon idiom--note get all but as a column vector!
ans =
5×1 char array
'H'
'e'
'l'
'l'
'o'
>>
For cellstr, things are a little different--
>> c={'Hello'} % build a cell string
c =
1×1 cell array
{'Hello'}
>> c % as expected the whole thing variable name is same
c =
1×1 cell array
{'Hello'}
>> whos c % now storage takes quite a lot more, though...
Name Size Bytes Class Attributes
c 1x1 122 cell
>> c(1) % but now the subscript is to the cell so is the cell
ans =
1×1 cell array
{'Hello'}
>> c{1} % but use the "curlies" to dereference to the char array content in the cell
ans =
'Hello'
>> c{1}(1) % and then can subscript with () the pieces of the base char array
ans =
'H'
>>
Then there's the new STRINGS class which is a lot like a cellstr in that subscripting returns the whole array element but unlike those must use specific methods for operations excepting subscripting is also dereference and subscript like a cellstr--
>> s="Hello"
s =
"Hello"
> s=string('Hello');
>> whos s
Name Size Bytes Class Attributes
s 1x1 134 string
>> s(1) % again a subscript returns the string array element content
ans =
"Hello"
>> s{1}(1) % looks like a cellstr to get to substring inside
ans =
'H'
>>
Probably what is happening altho we don't have code to see is that you're addressing a char() array with a subscript and only getting the portion, or the function you're passing a cellstr or string to isn't coded to handle them but doesn't have error handling to let you know that, either, hence it doesn't do anything useful (the above are both guesses; I've not looked at the function and you've shown no actual data but the crystal ball is just back from the shop so we'll give it a whirl...)

More Answers (0)

Categories

Asked:

on 27 Apr 2018

Edited:

on 28 Apr 2018

Community Treasure Hunt

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

Start Hunting!