How to replace NAN values in a string to a zero value
조회 수: 2 (최근 30일)
Hi I'm working with this text file and need to replace the "NAN" to a zero value
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)
Can anyone help me to see what I'm doing wrong please?
data=textscan(fileID,' %f %s','HeaderLines', 1,'Delimiter', ',');
%remove NAN from the VW data column
추가 답변 (2개)
Walter Roberson 2022년 12월 19일
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
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 2022년 12월 19일
편집: Bora Eryilmaz 님. 2022년 12월 19일
Your for-loop is not a valid MATLAB expression. It should probably be:
for i = 1:n
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;