How to Read and plot CSV file and delete infinity values from CSV file
古いコメントを表示
Hi
I want to read csv file and later plot it by using some formula.
I used csvread, but it doesn’t work as the file contains text (column title) and infinity values which MATLAB read as ∞.
I tried with readtable it can read (c=readtable('sensor.csv');. But again I can’t delete infinity values from the file. When I try with c(isnan(c))=0; or c(~any(isinf(c))) = 0; it always give error
“Undefined function 'isinf' or ‘isnan’ for input arguments of type 'table'”
Can anyone help how to read large csv file (xlsread doesn’t work, file size>1gb) and delete the infinity values which MATLAB read ∞.
Small section of large file is attached sensor.csv
Using Matlab R2017b
採用された回答
その他の回答 (3 件)
Bhaskar R
2020 年 2 月 20 日
opt = detectImportOptions('sensor.csv', 'MissingRule', 'fill', 'NumHeaderLines', 2);
T = readtable('sensor.csv', op);
header = {'Time', 'Sensor_A', 'Sensor_B', 'sensor_C', 'Sensor_D'};
T.Properties.VariableNames = header;
the cyclist
2020 年 2 月 20 日
編集済み: the cyclist
2020 年 2 月 20 日
I find that sometimes with these finicky imports it can be helpful to use the Import Data Tool.
I used that as a basis to make the following import script:
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [4, Inf];
end
% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 5);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = ",";
% Specify column names and types
opts.PreserveVariableNames = true;
opts.VariableNames = ["Time", "Sensor A", "Sensor B", "sensor C", "Sensor D"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
sensorDataTable = readtable(filename, opts);
% Convert to numeric
sensorDataArray = table2array(sensorDataTable);
2 件のコメント
the cyclist
2020 年 2 月 20 日
That script will put NaN where the input file has ????.
You could then do
sensorData(isnan(sensorData)) = Inf;
to convert to Infinity.
Walter Roberson
2020 年 2 月 25 日
Putting an explicit function header on it:
function sensorDataArray = ReadSensorTable(filename, dataLines)
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [4, Inf];
end
% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 5);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = ",";
% Specify column names and types
opts.PreserveVariableNames = true;
opts.VariableNames = ["Time", "Sensor A", "Sensor B", "sensor C", "Sensor D"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
sensorDataTable = readtable(filename, opts);
% Convert to numeric
sensorDataArray = table2array(sensorDataTable);
end
Walter Roberson
2020 年 2 月 21 日
0 投票
https://www.mathworks.com/help/matlab/ref/rmmissing.html can be used since R2016b. That is, you use readtable() and let the ??? be replaced by nan; after that rmmissing will remove the rows that have any nan, which seems to be what you are asking for.
There is also fillmissing() which tries to deduce what reasonable values might be in place of each nan.
4 件のコメント
Khalil
2020 年 2 月 25 日
Walter Roberson
2020 年 2 月 25 日
The techniques with import options showed by The Cyclist should translate the ??? to nan. You would rmmissing after that.
Khalil
2020 年 2 月 25 日
Walter Roberson
2020 年 2 月 25 日
I posted a version of his code, above, made into a function.
カテゴリ
ヘルプ センター および File Exchange で Text Files についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!