Why character or string array cannot be constructed like numerical array?

5 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020 年 4 月 2 日
編集済み: Stephen23 2020 年 4 月 3 日
For eample:
a=[]; a(1:3)=1
but you cannot make string of character the same way:
a=[]; a(1:3)="er"
it returns:
a =
NaN NaN NaN
or
a=[]; a(1:3)='er'
returns this error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Any idea to make string or character array?
  2 件のコメント
KSSV
KSSV 2020 年 4 月 2 日
Read about Cell. YOu can make cell of sting arrays.
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020 年 4 月 2 日
I prefer not to make cell.

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

採用された回答

Steven Lord
Steven Lord 2020 年 4 月 2 日
You have to start with the correct type of array.
a = "";
a(1:3) = "er"
  2 件のコメント
Stephen23
Stephen23 2020 年 4 月 2 日
編集済み: Stephen23 2020 年 4 月 3 日
This is not really equivalent, because
a = "";
actually defines a scalar string array (i.e. one element string (with no characters)), whereas
a = [];
defines an empty double (not a scalar double). To allocate an empty string array use:
a = string([]);
(the MATLAB documentation is very inconsistently written on this topic, sometimes describing "" as an "empty string" (which it isn't, it is scalar), whereas in other places one element strings are clearly described as scalar and also correctly and clearly states that "" defines a "scalar string". So apparently TMW have just proven that 0==1. I shudder to think of the justification that was invented for calling a 1x1 string "empty"...)
James Tursa
James Tursa 2020 年 4 月 2 日
And apparently a third category of this is <missing>. E.g.,
>> clear a
>> a(2) = "er"
a =
1×2 string array
<missing> "er"

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

その他の回答 (0 件)

カテゴリ

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