how to feed one string to array on NaN?

조회 수: 4 (최근 30일)
Abdulaziz Abutunis
Abdulaziz Abutunis 2017년 1월 19일
답변: Abdulaziz Abutunis 2017년 1월 19일
Dear MATLABERs :)
Is it possible to add a string to an array of NaN The next example does not work N = NaN(10,1); N(5,1)='Stop'; the result should be a column of NaN but the fifth element is Stop.
Thanks Aziz

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 19일
No. NaN is numeric, but 'Stop' is a character string. It is not possible to have numbers and characters in the same array.
You could use one of the following:
%cell array of mostly numeric
N = num2cell( NaN(10,1) );
N{5} = 'Stop';
or
%cell array of char vectors
N = cellstr( num2str( NaN(10,1) ) );
N{5} = 'Stop';
or, if you have R2016b or later,
%array of string data type
N = string(num2str(NaN(10,1)));
N(5) = 'Stop';

추가 답변 (2개)

James Tursa
James Tursa 2017년 1월 19일
No, you can't combine character strings as elements of a numeric array like that. To combine different data types in one variable, consider using a cell array.

Abdulaziz Abutunis
Abdulaziz Abutunis 2017년 1월 19일
Thank you James and Walter. Your both answer are of great help

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by