필터 지우기
필터 지우기

how to create empty string arrays and then populate them through arrayName(indexNumber) syntax

조회 수: 105 (최근 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

답변 (3개)

Ronald Whiddon
Ronald Whiddon 2019년 5월 10일
Declare an empty string array in this manner:
C=string.empty;
C(1)='hello';
C(2) = 'goodbye';

Dishant Arora
Dishant Arora 2014년 2월 20일
c{1} = 'hello';
  댓글 수: 2
Ronald Whiddon
Ronald Whiddon 2019년 5월 10일
This is a workaround, but it actually creates a cell array of strings.
Stephen23
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
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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by