How to replace NAN values in a string to a zero value

Hi I'm working with this text file and need to replace the "NAN" to a zero value
"Temperature","VW"
20.55,41.45
17.79,41.37
16.49,41.29
14.9,41.21
14.22,41.12
12.02,41.04
10.09,40.96,
9.47,"NAN"
8.623,"NAN"
6.855,"NAN"
6.197,40.57
17.08,40.49
14.19,40.42
17.66,40.38
I'm trying yo use for and if loops to remove and replace the NAN to a zero however after using the coded I have shared below, MATLAB is giving me this error:
Array indices must be positive integers or logical values.
Error in Replacing_NAN (line 11)
if VW(i)=='NAN'
Can anyone help me to see what I'm doing wrong please?
clear all
clc
fileID=fopen('F:\home\carolina\Met_files_code\Replacing_NAN_Test_File.txt','r');
data=textscan(fileID,' %f %s','HeaderLines', 1,'Delimiter', ',');
Temperature=data{1};
VW=data{2};
%remove NAN from the VW data column
n=length(VW)
for i=0,i<n
if VW(i)=='NAN'
'NAN'==0
end
end
disp(VW)

2 comentarios

VBBV
VBBV el 19 de Dic. de 2022
Editada: VBBV el 22 de Dic. de 2022
i = 1
while i < n
if strcmp(VW(i),"NAN")
VW(i) = 0;
end
i = i+1;
end
VBBV
VBBV el 19 de Dic. de 2022
it can work well in this situation if you use strcmp function.

Iniciar sesión para comentar.

 Respuesta aceptada

Try this:
m = readmatrix('Replacing_NAN_Test_File.txt')
m = 14×2
20.5500 41.4500 17.7900 41.3700 16.4900 41.2900 14.9000 41.2100 14.2200 41.1200 12.0200 41.0400 10.0900 40.9600 9.4700 NaN 8.6230 NaN 6.8550 NaN
Temperature = m(:, 1);
VW = m(:, 2);
% Replace nans with 0
VW(isnan(VW)) = 0
VW = 14×1
41.4500 41.3700 41.2900 41.2100 41.1200 41.0400 40.9600 0 0 0

3 comentarios

The OP is getting "NAN" as a string. isnan() will not work in that case.
But readmatrix() is converting the "NaN" character vectors to numeric nan.
Thank you for the help, very useful!

Iniciar sesión para comentar.

Más respuestas (2)

If you must use textscan() then skip most of that and use the TreatAsEmpty option; https://www.mathworks.com/help/matlab/ref/textscan.html#btghhyz-1-TreatAsEmpty
However I would suggest that you instead use readmatrix() with the TreatAsMissing option.
You need to decide what you want to do about the fact that the line
10.09,40.96,
has three variables instead of 2. The ExpectedNumVariables option might help; if not then if you use detectImportOptions you can set the number of variables in the options and you can set the rule to discard extra columns.
Bora Eryilmaz
Bora Eryilmaz el 19 de Dic. de 2022
Editada: Bora Eryilmaz el 19 de Dic. de 2022
Your for-loop is not a valid MATLAB expression. It should probably be:
for i = 1:n
end
When i == 0, VW(i) would not be valid MATLAB indexing since arrays don't have 0th element.
Also, 'NAN'==0 will not replace VM(i) with 0. Instead, do something like this:
if isequal(VW(i), 'NAN')
VW(i) = 0;
end

5 comentarios

I tried this and I keep getting the same error of :Array indices must be positive integers or logical values.
Then attach your data ('Replacing_NAN_Test_File.txt') with the paperclip icon after you read this:
Because the text file I made up and attached (copied from your post) in my answer below (scroll down) worked perfectly.
for i=0,i<n
is a valid MATLAB expression. It just doesn't do what a person with a C or C++ background might expect. It is equivalent to
for i=0 %it is valid to have a for over a scalar value
i < n %calculate and display the result of the comparison of 0 to n
end
Bora Eryilmaz
Bora Eryilmaz el 19 de Dic. de 2022
Editada: Bora Eryilmaz el 19 de Dic. de 2022
It is not a "valid" expression considering what the OP is trying to do. A technically valid expression doing the wrong thing is not really a valid expression.
You had written,
"Your for-loop is not a valid MATLAB expression."
But it is a valid MATLAB expression.
VW(1) = "NAN"
VW = "NAN"
n = 2
n = 2
for i=0,i<n
if VW(i+1)=='NAN'
'NAN'==0
end
end
ans = logical
1
ans = 1×3 logical array
0 0 0
MATLAB is willing to execute it, so it is a valid MATLAB expression. It just doesn't do what the user might hope.
valid, adjective:
(3) legally or officially acceptable.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Dic. de 2022

Editada:

el 22 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by