How to convert 2*1 cell array into 1*2char array

egr = Tier' % egr returns 'T2' 'S2' as 1*2 CELL
sgr = char(egr); % sgr returns T2
S2 as 2*2 char
My desired output should look like 'T2' 'S2' or 'T2','S2' in char

1 件のコメント

Guillaume
Guillaume 2018 年 10 月 22 日
Note that the only way a 2x1 cell array can be converted into a 1x2 char array is if each cell of the array contain a single character, or one cell contains 2 characters and the other none.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 23 日

0 投票

egr = {'T2' 'S2'};
sgr = ['''', strjoin(egr, ''' '''), ''''];
disp(sgr)
This will display as
'T2' 'S2'
which is to say that sgr will be a character vector that contains those literal characters, which appears to be what you are asking for.
However, this appears to have nothing to do with your switch statement, since you have
switch (T3)
and T3 has nothing to do with egr or sgr.

3 件のコメント

raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 23 日
yup, I made a mistake while i was posting it here, i was changing variables to avoid confusion, it is Switch(Sgr)
Walter Roberson
Walter Roberson 2018 年 10 月 23 日
Note that in your switch,
case {'T2' 'S2'}
would match if Sgr is either 'T2' or 'S2', not if it is the character vector "'T2' 'S2'" .
If you are trying to indicate by case {'T2' 'S2'} that the condition should match if the input contained either 'T2' or 'S2' then you should probably be using any(ismember()) or similar.
raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 23 日
Yeah, just now noticed that issue, thank you @ Walter Roberson

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 22 日
編集済み: madhan ravi 2018 年 10 月 22 日

1 投票

Tier={'T2';'S2'}'
egr=Tier
c=char(egr)

7 件のコメント

raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 22 日
please read my question again, because c={'2';'4'} will return 2*1 cell array i.e {'2'}{'4'} and char (C) will return 2*1 char array '2' '1'
madhan ravi
madhan ravi 2018 年 10 月 22 日
See edited answer now
Guillaume
Guillaume 2018 年 10 月 22 日
It's really not clear what you want. I would have thought that madhan's answer was what you asked for.
Please give a full example of an input and the desired output, using valid matlab syntax (as madhan did).
raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 22 日
編集済み: raghavendra kandukuri 2018 年 10 月 22 日
egr= Tier'
sgr= char(egr)
if(strcmp(view,'new')==1)
switch (T3)
case {'T2' 'S2'}
Tier='T2 (SII)';
case{'T3' 'S3' 'B3' 'C3' 'H3' '3D'}
Tier='T3 (SIIIA)';
case{'I4' 'R4' 'ID'}
Tier='IT4 (SIIIB)';
case{'F4' 'S4' 'C4' 'H4' '4D'}
Tier='FT4 (SIV)';
case{'C5' 'S5' 'H5'}
Tier='FT4 (SV)';
otherwise
Tier='';
end
end
% As sgr is returning T2 S2 as 2*2 Array(vertically), so when it is passed in to switch condition, it is not meeting any of the switch cases.
desired functionality is to stop at first case, I hope i was clear this time.
madhan ravi
madhan ravi 2018 年 10 月 22 日
編集済み: madhan ravi 2018 年 10 月 22 日
Thank you Guillame, @Raghavendra unfortunately we can’t see your comment for some reason
Image Analyst
Image Analyst 2018 年 10 月 22 日
Try again - I rescued it from the spam quarantine.
raghavendra kandukuri
raghavendra kandukuri 2018 年 10 月 22 日
Thank You @ Image Analyst

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by