Replace NaN with Blanks
218 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Abdullah Azzam
el 7 de Feb. de 2020
Comentada: David
el 5 de Jun. de 2024
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270478/Matrix.png)
Thanks in advance
1 comentario
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.
Respuesta aceptada
Walter Roberson
el 7 de Feb. de 2020
Editada: Walter Roberson
el 7 de Feb. de 2020
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
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.
Más respuestas (2)
dpb
el 7 de Feb. de 2020
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.
0 comentarios
Opeyemi Kehinde
el 5 de Dic. de 2021
fillmissing(V1, 'constant', [])
will this work to make NaN an empty entry
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!