How to convert string or chars to string arrays withing for loop?

2 ビュー (過去 30 日間)
Element
Element 2019 年 11 月 4 日
回答済み: Element 2019 年 11 月 4 日
How could I convert message to string array withing for loop? Each string in array has to be 4 characters long. For example:
message = 'SOMETEXT';
for i = 1:4:length(message)
%some code here
end
Outcome would be:
out = ["SOME", "TEXT"];
Many thanks

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 4 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 4 日
message ='SOMETEXT';
l=1;
for i=1:4:length(message)
result{l}=message(i:i+3);
l=l+1;
end
result
You asked for for loop (specifically)

その他の回答 (2 件)

Guillaume
Guillaume 2019 年 11 月 4 日
編集済み: Guillaume 2019 年 11 月 4 日
An easy way, with no loop, a bit similar to Stephan's answer:
%demo data:
in = char(randi(double('AZ'), 1, 28)) %random string of 28 characters
out = string(reshape(in, 4, [])')

Element
Element 2019 年 11 月 4 日
Thanks guys, very helpfull.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by