MATLAB Answers

how to feed one string to array on NaN?

23 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2017
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';

  0 Comments

Sign in to comment.

More Answers (2)

James Tursa
James Tursa on 19 Jan 2017
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.

  0 Comments

Sign in to comment.


Abdulaziz Abutunis
Abdulaziz Abutunis on 19 Jan 2017
Thank you James and Walter. Your both answer are of great help

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags

No tags entered yet.