How to transpose a cell array converted into a character arrray?
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2018-12-10
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.
回答(2 个)
Fangjun Jiang
2018-12-10
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?
0 个评论
Stephen23
2018-12-10
编辑:Stephen23
2018-12-10
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 个评论
Stephen23
2018-12-11
编辑:Stephen23
2018-12-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!