How to transpose a cell array converted into a character arrray?
Show older comments
Hi!
I have a cell array such as:

Using xlsread I store the data in an array named Colours.
The type of the array is cell.
So I want to transpose the array from vertical to horizontal usign : C = colours .'
Now I have a horizontal array (a row) - Color: Green Blue Red Yellow
And if I convert it into a charr array the results are like : GBRY
RLEE
.......
In other words, MATLAB converts the words one by one, not the elements of the array?
Any suggestions why ???
8 Comments
Adam
on 10 Dec 2018
What result are you expecting from converting to a char array? Also it would help if you showed some actual code. Your screenshot shows something with a lot of empty cells. Is that your C cell array? If so what size/shape is it?
John Rebbner
on 10 Dec 2018
madhan ravi
on 10 Dec 2018
The below ?:
c={'Green' ,'Blue', 'Red'};
char(c).'
Jan
on 10 Dec 2018
@John: This is not clear, because it is no valid Matlab code:
Colours: 'Green' ' Blue' ' Red' .....
class(Colours) : char
Using char to convert a cell string to a char matrix does exactly what is advertised in the documentation: It conatenates the char vectors vertically and pads with spaces on the rigth side.
c = {'Green' ,'Blue', 'Red'};
char(c)
c = ['Green'; ...
'Blue '; ...
'Red ']
Transposing this is not really useful.
Please explain, what you want to achieve.
@John Rebner: here is a character vector with one row:
'GreenBlueRed'
What you showed
'Green' ' Blue' ' Red' ...
is three character vectors, apparently in a comma-separated list (e.g. as used to define a cell array or input arguments to a function). What you showed is certainly not one character vector.
What is wrong with just keeping those character vectors in the cell array?
John Rebbner
on 10 Dec 2018
Jan
on 10 Dec 2018
Name_XT = empties [];
This is no valid Matlab syntax.
Name_XT = NameX(end_ln:end);
Name_XT(cellfun('isempty',Name_XT)) = [];
Name_Add_char = char(Name_Add);
It is not clear, what you mean by "horizontal character array". Please post an example instead of letting us guess.
Answers (2)
Fangjun Jiang
on 10 Dec 2018
a={'Green','Blue','Red'};
b=a';
aa=char(a);
bb=char(b);
isequal(aa,bb)
Both aa and bb are 3x5 char array and they are identical.
What do you really want? What things can't be done with a or aa?
Converting to a horizontal character array is easy with a comma-separated list:
>> C = {'Green','Blue','Red'}; % orientation is irrelevant.
>> V = [C{:}] % this is all you need to do.
V = 'GreenBlueRed'
>> class(V)
ans = char
>> isrow(V)
ans = 1
Or perhaps you want something like this:
>> M = reshape(char(C).',1,[])
M = 'GreenBlue Red '
>> class(M)
ans = char
>> isrow(M)
ans = 1
In your comment your wrote "This is what I Expect by converting the cell array into a character"
Colours: 'Green' ' Blue' ' Red' .....
class(Colours) : char
but so far you have ignored us telling you that actually what you showed us are three separate character vectors (without any container variable, e.g. cell or string array). So far what you expect a character vector to be and what they actually are do not correspond.
I suspect that what you really want is a string array:
>> S = string(C).'
S = 1x3 string array
"Green" "Blue" "Red"
3 Comments
John Rebbner
on 11 Dec 2018
Edited: John Rebbner
on 11 Dec 2018
John Rebbner
on 11 Dec 2018
When you read the char documentation it clearly states "If A is a cell array of character arrays, then char converts the cell array to a character array. Each row from each character array in the cell array becomes a row in C, automatically padded with blank spaces as needed." This means if A is a cell vector then its orientation is totally irrelevant, because each cell will simply become one row of the output character array.
However your explanation and your examples do not match each other, and because you do not use valid MATLAB syntax we have no idea what variables you actually have.
Lets have a look at the two possibilities, first with a cell array:
>> Name = {'MATE';'GEAR';'FIVE';'SLOW'}; % Cell array.
>> char(Name)
ans =
MATE
GEAR
FIVE
SLOW
>> char(Name.') % transpose is irrelevant, for the reason I gave above.
ans =
MATE
GEAR
FIVE
SLOW
So it appears that your Names array is actually a character array (not a cell array like you wrote), in which case the char call is totally superfluous anyway:
>> Name = ['MATE';'GEAR';'FIVE';'SLOW'] % Char array, NOT a cell array!
Name =
MATE
GEAR
FIVE
SLOW
>> Name.'
ans =
MGFS
AEIL
TAVO
EREW
So far no surprises: transposing a char array gives the transpose of that char array. All of this indicates that your Name array is actually a character array, and not a cell array as you wrote: "this is my array of cell type"'. You can check the class quite easily by using this command:
class(Name)
and then tell us what its output is.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!