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
Give us an example of itemNames with three items.
/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>...
I believe the second item is an empty double. Try
class( itemName{2} )
/ per
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>...
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
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>...
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
it works very well,
Kind Regards,
Manal
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
Just a tiny comment:
> A = cellfun(@ischar,time0);
Faster:
A = cellfun('isclass', time0, 'char');
Jan