フィルターのクリア

How to split characters of a cell/string in matlab?

2 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2023 年 3 月 3 日
コメント済み: Stephen23 2023 年 3 月 4 日
I have a question about a code. I have a string array and I would like to split it into pairs, depending the number of elements.
for example I have string 'CHARLES'. I would like to have a cell array with the elements C={CH, HA, AR, RL, LE, ES}. (I mean one cell array with 1x6 (length-1) dimensions)
my code is:
g='CHARLES'
length(g)
for i=1:(length(g)-1)
C=horzcat(g(i),g(i+1))
%charArray(i) = [AL(i){:}]
end
How must I modify my code? Could you please help me?

採用された回答

Voss
Voss 2023 年 3 月 3 日
g='CHARLES';
C = cellstr([g(1:end-1); g(2:end)].').'
C = 1×6 cell array
{'CH'} {'HA'} {'AR'} {'RL'} {'LE'} {'ES'}
  2 件のコメント
Ivan Mich
Ivan Mich 2023 年 3 月 4 日
編集済み: Ivan Mich 2023 年 3 月 4 日
ok thank you @Voss. One more question. How could I use this command for a cell array?
I mean I have a cell array (2000x1 dimensions) and I want to use this command for each element of this array?
I write this code
for i=1:numel(g)
C(i) = cellstr([g(i)(1:end-1); g(i)(2:end)].').'
end
but no use...
Could you pelase help me?
Stephen23
Stephen23 2023 年 3 月 4 日
Use curly braces to access the content of cell arrays, not parentheses:
C = {'charles','hello','world'};
for k = 1:numel(C)
C{k} = cellstr([C{k}(1:end-1); C{k}(2:end)].').';
end
Checking the content:
C{:}
ans = 1×6 cell array
{'ch'} {'ha'} {'ar'} {'rl'} {'le'} {'es'}
ans = 1×4 cell array
{'he'} {'el'} {'ll'} {'lo'}
ans = 1×4 cell array
{'wo'} {'or'} {'rl'} {'ld'}
The ways to access cell arrays are explained here:

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by