How to use cellstr() without losing whitespace character?

8 ビュー (過去 30 日間)
Michael Kainzbauer
Michael Kainzbauer 2019 年 9 月 22 日
コメント済み: Abdullah Al Baki 2022 年 6 月 15 日
Hi,
i need cellstr NOT to delete ending whitespace. Some strings have whitespace and i need those to stay alive.
Btw it's weird that cellstr does not perform a full strtrim but only deletes ending whitespace character.
{' test '} would do the job for just 1 string, how do i perform this on hundrets without a loop?
What is the best way to do that?
thx
  2 件のコメント
Bruno Luong
Bruno Luong 2019 年 9 月 22 日
The question needs to be illustrated with examples.
Michael Kainzbauer
Michael Kainzbauer 2019 年 11 月 8 日
a = cellstr(' test ') saves {' test'}
a = {' test '} saves {'test'}
i call this inconsistent ;)
a = num2cell(' test ',2) saves {' test '} what i needed

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

採用された回答

Bruno Luong
Bruno Luong 2019 年 9 月 22 日
Pay attention to the difference between the two last commands
>> c1={'1' '12' '123'}
c1 =
1×3 cell array
{'1'} {'12'} {'123'}
>> c2=char(c1)
c2 =
3×3 char array
'1 '
'12 '
'123'
>> c3=cellstr(c2)
c3 =
3×1 cell array
{'1' }
{'12' }
{'123'}
>> c4=num2cell(c2,2)
c4 =
3×1 cell array
{'1 '}
{'12 '}
{'123'}

その他の回答 (2 件)

madhan ravi
madhan ravi 2019 年 9 月 22 日
Wanted = num2cell(your_char_array,2)
  1 件のコメント
Michael Kainzbauer
Michael Kainzbauer 2019 年 9 月 22 日
yes, seems to do what i need. thx
num2cell([' test ';'test ';' a '],2)
ans =
' test '
'test '
' a '

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


dpb
dpb 2019 年 9 月 22 日
編集済み: dpb 2019 年 9 月 22 日
Try the new string class...
s=string(your_char_array);
If you must end up with cellstr() array, then cast that result.
s=cellstr(string(your_char_array));

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by