Replace NaN with Blanks

249 次查看(过去 30 天)
Hi guys I have a double matrix as shown in the attached picture that I am trying to replace all NaN values with Empty spaces so that the over all matrix dimension will be maintained. However, when I looked through the function and suggested solutions non of them is actully working what possible code can be used.
Matrix.PNG
Thanks in advance

采纳的回答

Walter Roberson
Walter Roberson 2020-2-7
编辑:Walter Roberson 2020-2-7
It is not possible to have a numeric array that has blanks in it.
You can use
fillmissing(V1, 'constant', 0)
to replace the NaN entries with 0... or you could do
V1(isnan(V1)) = 0;
for the same effect.
If you need a blank then you will need to convert to cell array, such as
V1c = num2cell(V1);
mask = cellfun(@(C) all(isnan(C)), V1c);
V1c(mask) = {' '};
If you do this, then the variable browser will show ' ' (including the quotes) in those locations but the quote marks are not part of the content. disp(V1c) would show ugly things like
{[ 1]} {[ 1]} {[ 1]} {[2]} {[3]}
{' '} {[ 3]} {[ 2]} {[1]} {[3]}
{[ 2]} {' '} {[ 3]} {[3]} {[2]}
{[ 3]} {[ 2]} {[ 3]} {[3]} {[2]}
{[ 3]} {[ 3]} {' '} {[3]} {[1]}
  4 个评论
Abdullah Azzam
Abdullah Azzam 2020-2-8
Ok. Then how may I import an excel sheet that contain both numaric and string value into the matlab. Note if it isn't possible I don't mind if the numric were saved as strings
Walter Roberson
Walter Roberson 2020-2-8
readtable() is what is recommended these days.
Or you could xlsread() . The first input will contain only numeric parts, the second output will contain text without numbers, and the third will contain both. But readtable() is typically better.
In some cases it might be more suitable to use readcell() for R2019b and later.

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2020-2-7
Can't be done--a double array has to be regular in both dimension; can't have holes.
Only possibility is to convert to a cell array.

Opeyemi Kehinde
Opeyemi Kehinde 2021-12-5
fillmissing(V1, 'constant', [])
will this work to make NaN an empty entry
  2 个评论
Walter Roberson
Walter Roberson 2021-12-5
Let us test:
V1 = [1 2 nan 3; 4 nan 5 6]
V1 = 2×4
1 2 NaN 3 4 NaN 5 6
fillmissing(V1, 'constant', [])
Error using fillmissing/checkConstantsSize (line 740)
Fill constant must be a scalar or a 4-element vector.

Error in fillmissing/parseInputs (line 680)
intConstOrWindowSize = checkConstantsSize(A,AisTable,false,intConstOrWindowSize,dim,dataVars,'');

Error in fillmissing (line 158)
[A,AisTable,intM,intConstOrWinSize,extM,x,dim,dataVars,ma,maxgap] = parseInputs(A,fillMethod,varargin{:});
So... NO.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by