How do I remove NaN's from my data?

I was given a csv file and I used this code below to read it:
clear;
fclose all;
fileID = fopen('accidents_2017.csv');
text1 = textscan(fileID,'%s%s%s%s%s%s%d%d%s%d%d%d%d%f%f','HeaderLines',1,'Delimiter',{','},'EmptyValue',NaN);
fclose(fileID);
How do I plot this data? My teacher never gave a function of how to plot with data values. I already converted all the data into numbers using str2double(text1). What should I do?

回答 (2 件)

the cyclist
the cyclist 2021 年 2 月 22 日

0 投票

If your numeric vector is called "x", then
x(isnan(x)) = [];
will remove the NaN values.

7 件のコメント

Sarah Mullin
Sarah Mullin 2021 年 2 月 22 日
what is the x for on the outside of the parenthesis?
Walter Roberson
Walter Roberson 2021 年 2 月 22 日
x here should be replaced with the name of the variable you want to remove nan from.
the cyclist
the cyclist 2021 年 2 月 22 日
Here is some insight into what is happening in the code:
% Input
x = [4 5 nan 6 7 nan 8]
x = 1×7
4 5 NaN 6 7 NaN 8
% isnan(x) creates a "logical" vector of true/false values (1's and 0's), with "true" where x is NaN
isnan(x)
ans = 1x7 logical array
0 0 1 0 0 1 0
% x(isnan(x)) = [] sets those "true" locations to empty ... in other words it removes them
x(isnan(x)) = []
x = 1×5
4 5 6 7 8
Sarah Mullin
Sarah Mullin 2021 年 2 月 22 日
I tried that and it says "Undefined function 'isnan' for input arguments of type 'cell'."
the cyclist
the cyclist 2021 年 2 月 22 日
If you are using the output of the str2double function (as you mentioned), then it should be a double-precision numeric, and not a cell.
Maybe you can upload the data in a MAT file?
Image Analyst
Image Analyst 2021 年 2 月 22 日
Try readmatrix() instead of fopen(), textscan(), and fclose().
Walter Roberson
Walter Roberson 2021 年 2 月 22 日
As this is roughly the 4th or 5th posting on the same topic:
as far as I have been able to determined, this is an assignment and the poster is required to use textscan() with the format indicated, even though it is not the best tool.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2021 年 2 月 22 日

0 投票

Don't use
str2double(text1)
Use
str2double(text1{1})
replacing the 1 with 2, 3, 4, 5, 6, or 9 as needed (the column number to extract.)

質問済み:

2021 年 2 月 22 日

コメント済み:

2021 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by