how to create empty string arrays and then populate them through arrayName(indexNumber) syntax
275 ビュー (過去 30 日間)
古いコメントを表示
The following works for numbers:
B=[];
B(1)=10;
but how to make it work for strings such as
C=[''];
C(1)='hello';% does NOT work
0 件のコメント
回答 (3 件)
Ronald Whiddon
2019 年 5 月 10 日
Declare an empty string array in this manner:
C=string.empty;
C(1)='hello';
C(2) = 'goodbye';
0 件のコメント
Dishant Arora
2014 年 2 月 20 日
c{1} = 'hello';
2 件のコメント
Ronald Whiddon
2019 年 5 月 10 日
This is a workaround, but it actually creates a cell array of strings.
Stephen23
2019 年 5 月 10 日
編集済み: Stephen23
2019 年 5 月 10 日
"This is a workaround, but it actually creates a cell array of strings. "
At the time this question was posed (and answered) the string class did not exist (it was only introduced in R2016b). Each element of a character array stores only one character, so (until the string class was introduced) the only way to store multiple characters using a scalar index (as the question poses) is to use a cell array. From that persepctive it is not a "workaround", it is the correct solution for versions of MATLAB before R2016b.
Nigel Nettheim
2023 年 6 月 9 日
You can initialise an empty string array with:
s = strings(0)
to which the response is:
s =
0×0 empty string array
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!