필터 지우기
필터 지우기

I want to change cell array to string.

조회 수: 11 (최근 30일)
niniki
niniki 2022년 2월 28일
댓글: Stephen23 2022년 2월 28일
I want to change cell array to string.
I have a 1x6 cell array.
{'abc = 1'}
{'def = 2'}
{'cba = 3'}
{'fed = 4'}
{'sag = 5'}
{'dfg = 6'}
I used strjoin() for this.
abc = 1 def = 2 cba = 3 fed = 4 sag = g dfg = 6
In this way, all characters are connected and printed.
I want to produce the results as below.
abc = 1
def = 2
cba = 3
fed = 4
sag = 5
dfg = 6

채택된 답변

Voss
Voss 2022년 2월 28일
편집: Voss 2022년 2월 28일
Here are a few different things you can try, depending on your purposes:
C = {'abc = 1' 'def = 2' 'cba = 3' 'fed = 4' 'sag = 5' 'dfg = 6'}
C = 1×6 cell array
{'abc = 1'} {'def = 2'} {'cba = 3'} {'fed = 4'} {'sag = 5'} {'dfg = 6'}
% 2D character array, only works if all elements of C are the same length
char_array = vertcat(C{:})
char_array = 6×7 char array
'abc = 1' 'def = 2' 'cba = 3' 'fed = 4' 'sag = 5' 'dfg = 6'
% string array:
str = string(C.')
str = 6×1 string array
"abc = 1" "def = 2" "cba = 3" "fed = 4" "sag = 5" "dfg = 6"
str = string(C(:))
str = 6×1 string array
"abc = 1" "def = 2" "cba = 3" "fed = 4" "sag = 5" "dfg = 6"
% column vector cell array
cell_column = C.'
cell_column = 6×1 cell array
{'abc = 1'} {'def = 2'} {'cba = 3'} {'fed = 4'} {'sag = 5'} {'dfg = 6'}
cell_column = C(:)
cell_column = 6×1 cell array
{'abc = 1'} {'def = 2'} {'cba = 3'} {'fed = 4'} {'sag = 5'} {'dfg = 6'}
% just print the original cell array
fprintf('%s\n',C{:})
abc = 1 def = 2 cba = 3 fed = 4 sag = 5 dfg = 6

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 2월 28일
try this:
A=[{'abc = 1'},{'def = 2'},{'cba = 3'},{'fed = 4'},{'sag = 5'},{'dfg = 6'}];
str=string(A)';
fprintf('\n%s\n',str);
abc = 1 def = 2 cba = 3 fed = 4 sag = 5 dfg = 6
  댓글 수: 1
Stephen23
Stephen23 2022년 2월 28일
Converting to string is completely superfluous, as _'s answer correctly shows:
C = {'abc = 1' 'def = 2' 'cba = 3' 'fed = 4' 'sag = 5' 'dfg = 6'};
fprintf('%s\n',C{:})
abc = 1 def = 2 cba = 3 fed = 4 sag = 5 dfg = 6

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by