필터 지우기
필터 지우기

How to transpose a cell array converted into a character arrray?

조회 수: 14 (최근 30일)
John Rebbner
John Rebbner 2018년 12월 10일
편집: Stephen23 2018년 12월 11일
Hi!
I have a cell array such as:
Untitled.png
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
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.
Stephen23
Stephen23 2018년 12월 11일
편집: Stephen23 2018년 12월 11일
@John Rebbner: please do not close question which have answers. It is not appreciated when you unilaterally decide that our efforts to help you should be discarded.

댓글을 달려면 로그인하십시오.

답변 (2개)

Fangjun Jiang
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?

Stephen23
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
John Rebbner
John Rebbner 2018년 12월 11일
I have tryied to transpose Name array without coma after it, I mean : Name_new = (Name)' but the results are the same ....
Stephen23
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by