Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert to cell array into a character array or cell array of strings

1,815 views
Skip to first unread message

Manal

unread,
May 17, 2009, 9:24:01 PM5/17/09
to
Hi

I have this horizontal array of names:
Name Size Bytes Class Attributes

itemNames 1x70 7428 cell


I need to convert this into a a character array or cell array of strings.

I tried:

S = char( itemNames)
??? Error using ==> char
Cell elements must be character arrays.

I tried also:

S = cellstr( itemNames)
??? Error using ==> cellstr at 34
Input must be a string.

I appreciate your help,

thanks,

Manal

per isakson

unread,
May 17, 2009, 10:00:03 PM5/17/09
to
"Manal " <ne...@mathworks.com> wrote in message <guqdbh$qci$1...@fred.mathworks.com>...

Give us an example of itemNames with three items.
/per

Manal

unread,
May 17, 2009, 11:01:02 PM5/17/09
to
Hi Per

The data I am loading is dynamic. I read a horizontal array from a file:

itemNamesFile = load (itemNamesFileName);
itemNames = itemNamesFile .('itemNames');

The file opens automatically as a structure of one field with name 'itemNames'. this field is an array of one cell, where the names are stored. The file is originally saved from an excel sheet, and imported to matlab and transposed to be horizontal. I later saved it to a .mat file. The contents of the file will vary, So, I always need to read from files, I can not define static data. I copied the initial three first cells as shown in the Variable editor

'Item 1' [] 'Item 3'

the second one is an empty string,

thank you very much,

"per isakson" <poi.n...@bimDOTkthDOT.se> wrote in message <guqff3$7ub$1...@fred.mathworks.com>...

per isakson

unread,
May 18, 2009, 1:00:03 AM5/18/09
to
"Manal " <ne...@mathworks.com> wrote in message <guqj1e$hc8$1...@fred.mathworks.com>...

> Hi Per
>
> The data I am loading is dynamic. I read a horizontal array from a file:
>
> itemNamesFile = load (itemNamesFileName);
> itemNames = itemNamesFile .('itemNames');
>
> The file opens automatically as a structure of one field with name 'itemNames'. this field is an array of one cell, where the names are stored. The file is originally saved from an excel sheet, and imported to matlab and transposed to be horizontal. I later saved it to a .mat file. The contents of the file will vary, So, I always need to read from files, I can not define static data. I copied the initial three first cells as shown in the Variable editor
>
> 'Item 1' [] 'Item 3'
>
>
>
> the second one is an empty string,
>
> thank you very much,

I believe the second item is an empty double. Try

class( itemName{2} )

/ per

Manal

unread,
May 18, 2009, 3:01:03 AM5/18/09
to
Yes it is:
ans =

double


How can I handle empty cells then? do I loop and explicitly convert types? if so, what is the conversion function that should work (is cast( itemName{i}, 'char') ok)? is there a function that works on all elements of the array at once?

I tried this:

for i=1:itemsCount
new_itemNames(j) = cast( itemNames(1,j), 'char')
end
??? Error using ==> cast


Cell elements must be character arrays.


I also tried:

for i=1:itemsCount
new_itemNames{j} = cast(itemNames{1, j}, 'char')
end

however, when I try it individually it works:
new_itemNames{1} = cast(itemNames{1, 1}, 'char')
new_itemNames{2} = cast(itemNames{1, 2}, 'char')
new_itemNames{3} = cast(itemNames{1, 3}, 'char')

any ideas please? I am so confused about the notation,

thank you very much for your help,

"per isakson" <poi.n...@bimDOTkthDOT.se> wrote in message <guqq0j$s8e$1...@fred.mathworks.com>...

Jos

unread,
May 18, 2009, 6:13:01 AM5/18/09
to
"Manal " <ne...@mathworks.com> wrote in message <gur13f$ri7$1...@fred.mathworks.com>...
<SNIP ... cell troubles ...

It always helped me to think of a cell array as cabinet. Each element of the cabinet is drawer, and each drawer can have content (which can be anything!). The "normal" notation () for a cell array then refers to the drawer, whereas the curly-braces {} refer to the contents of a drawer.

% Example
MyCellArray = {1 [] 'hello'} % create a cell array
MyCellArray(2) % drawer #2
MyCellArray{3} % contents of drawer #3 (which is in this example a string)
MyCellArray(4) = {MyCellArray{1} + 1}
% which translates as: get the contents of drawer #1, add 1 to it, put it in a drawer and make drawer #4 of my cabinet that drawer. This has the a similar effect as:
MyCellArray{5} = MyCellArray{1} + 1


As for your problem

C = {'Item 1' [] 'Item 3'}
C{2} % an empty double array
% Use cellfun to find all empty cells at once:
% make a logical vector with trues where the contents of a drawer is empty
q = cellfun('isempty',C)

% replace the drawers by a new drawer holding an empty string
C(q) = {''}
% and convert into a char array
D = char(C)

hth
Jos

Manal

unread,
May 19, 2009, 12:59:02 AM5/19/09
to
Thank you very much for your detailed explanation. I was looking around for the meaning of {} vs (), and your explanation should be in the manual (if not already and I just couldn't locate it)

it is working now, thanks to you

one greedy question, is there a way to have the x and y tick labels in the plot images, tilted or vertical or any other angle rather than the normal horizontal writing?

Thank you very much for your help,

Manal

"Jos " <#10...@fileexchange.com> wrote in message <gurcbd$bq8$1...@fred.mathworks.com>...

ngre...@gmail.com

unread,
Jun 3, 2009, 4:20:31 PM6/3/09
to
On May 18, 9:59 pm, "Manal " <n...@mathworks.com> wrote:
> Thank you very much for your detailed explanation. I was looking around for the meaning of {} vs (), and your explanation should be in the manual (if not already and I just couldn't locate it)
>
> it is working now, thanks to you
>
> one greedy question, is there a way to have the x and y tick labels in the plot images, tilted or vertical or any other angle rather than the normal horizontal writing?
>
> Thank you very much for your help,
>
> Manal
>
> "Jos " <#10...@fileexchange.com> wrote in message <gurcbd$bq...@fred.mathworks.com>...
> > "Manal " <n...@mathworks.com> wrote in message <gur13f$ri...@fred.mathworks.com>...

> > <SNIP ... cell troubles ...
>
> > It always helped me to think of a cell array as cabinet. Each element of the cabinet is drawer, and each drawer can have content (which can be anything!). The "normal" notation () for a cell array then refers to the drawer, whereas the curly-braces {} refer to the contents of a drawer.
>
> > % Example
> > MyCellArray = {1 [] 'hello'} % create a cell array
> > MyCellArray(2) % drawer #2
> > MyCellArray{3} % contents of drawer #3 (which is in this example a string)
> > MyCellArray(4) = {MyCellArray{1} + 1}
> > % which translates as: get the contents of drawer #1, add 1 to it, put it in a drawer and make drawer #4 of my cabinet that drawer. This has the a similar effect as:
> > MyCellArray{5} = MyCellArray{1} + 1
>
> > As for your problem
>
> > C = {'Item 1' [] 'Item 3'}
> > C{2} % an empty double array
> > % Use cellfun to find all empty cells at once:
> > % make a logical vector with trues where the contents of a drawer is empty
> > q = cellfun('isempty',C)
>
> > % replace the drawers by a new drawer holding an empty string
> > C(q) = {''}
> > % and convert into a char array
> > D = char(C)
>
> > hth
> > Jos

Hey, actually there are some tilting methods for tick labels posted in
the matlab file exchange.
http://www.mathworks.com/matlabcentral/fileexchange/3486
That's for rotating the x-tick labels
http://www.mathworks.com/matlabcentral/fileexchange/20529
And this one is the slightly modified one for y-tick labels.

Have fun!
-Nathan

Manal

unread,
Jun 16, 2009, 3:24:01 AM6/16/09
to
Thank you very much Nathan

it works very well,

Kind Regards,

Manal

Dove

unread,
Oct 22, 2010, 4:08:04 PM10/22/10
to
Hi Jos,

Just want to let you know that it's extremely helpful. I was working with a cell array with mixed types of drawers. I want to assign the contents in the character drawers as NaNs and couldn't find a way. Inspired by your reply, I did (time0 is the mixed cell array)

A=cellfun(@ischar,time0);
time0(A)={nan};

Now I can use cell2mat(time0) to get a double array.

Thank you very much!

Dove

Jan Simon

unread,
Oct 22, 2010, 5:03:04 PM10/22/10
to
Dear Dove,

Just a tiny comment:
> A = cellfun(@ischar,time0);
Faster:
A = cellfun('isclass', time0, 'char');

Jan

0 new messages