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

1 comentario

David
David el 5 de Jun. de 2024
This is not a "fix it in Matlab" answer, but I've been generating tables in Matlab to use in Word for example. When I build the table matrix, I insert NaN's in every cell that I want blank. Then in Word, I use "find and replace" to replace all those with something else; i.e., '---'. This idea should work in Excel too, although I've not tested it there.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Feb. de 2020
Editada: Walter Roberson el 7 de Feb. de 2020

2 votos

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 comentarios

Abdullah Azzam
Abdullah Azzam el 7 de Feb. de 2020
What I am trying to do is combining 2 array one contain string and the other contains the value. But when I try to do that the NaN or empty space overwrite the other spaces.
Assuming the same example above but some of the NaNs has string on them in another array with same dimension. Is there away to combine these 2 array as one.
Walter Roberson
Walter Roberson el 8 de Feb. de 2020
You can use a cell array, or you can use a table, or you can use a struct.
However, it is impossible in MATLAB to have a numeric array that contains (for example)
-10 7 trace
-4 -2 none
3 9 5 mm
Numeric arrays cannot contain characters. Non-numeric arrays such as cell arrays do not display anything like the above example by default -- to display like that you would have to write a small bit of code to do the formatting into characters.
Abdullah Azzam
Abdullah Azzam el 8 de Feb. de 2020
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 el 8 de Feb. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (2)

dpb
dpb el 7 de Feb. de 2020

0 votos

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 el 5 de Dic. de 2021

0 votos

fillmissing(V1, 'constant', [])
will this work to make NaN an empty entry

2 comentarios

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.
Opeyemi Kehinde
Opeyemi Kehinde el 10 de Dic. de 2021
Thank you!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2017a

Preguntada:

el 7 de Feb. de 2020

Comentada:

el 5 de Jun. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by