Read the third column of a .csv file

조회 수: 20 (최근 30일)
Alex Perrakis
Alex Perrakis 2022년 4월 26일
답변: Johannes Hougaard 2022년 4월 26일
Hello Guys and Girls,
i have following .csv file and i am trying to import only the 3rd column which is a temperature. The problem is that although i read it with readtable for some reasons my delimiter does not work and it shows the time and temperature in one cell
My code until now is as follows:
URL = uigetdir('C:\_Daten\Messungen\');
measurements = dir(URL); measurements(1:2) = [];
file=fullfile( URL, measurements.name);
fid = fopen(file, 'rt');
%a = textscan(fid, '%{yyyy-MM-dd}D %{hh:mm:ss.SSS}T %f ','Delimiter',' ');
A=readtable(file,'Format','%s%s%n','Delimiter',' ');
B=table2array(A(:,2));
C=split(B,' ');
% B=table2array(A(:,3));
% Data = str2double(strrep(B, ',', '.'));
% format short g
fclose(fid);
Thanks very much for the help!
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 4월 26일
give the option for decimal separator being ','
Alex Perrakis
Alex Perrakis 2022년 4월 26일
Hello Walter,
i think 'DecimalSeparator' does not work with readtable, or maybe i am doing sometimg wrong.
URL = uigetdir('C:\_Daten\Messungen\');
measurements = dir(URL); measurements(1:2) = [];
file=fullfile( URL, measurements.name);
fid = fopen(file, 'rt');
a = textscan(fid, '%q%q%f ','Delimiter','\b\t');
stringData = strrep(fid,',','.');
A=readtable(file,'DecimalSeparator',',','Format','%s%s%n','Delimiter','\b\t');
B=table2array(A(:,2));
C=split(B,' ');
% B=table2array(A(:,3));
% Data = str2double(strrep(B, ',', '.'));
% format short g
fclose(fid);

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2022년 4월 26일
hello
you can use the import wizard to help you create the right import options for readtable
see demo below - temperature is now in C
file = 'MS1.csv';
A = importfile(file);
C=table2array(A(:,3)); % temperature
function T = importfile(filename, dataLines)
%IMPORTFILE Import data from a text file
% MS1 = IMPORTFILE(FILENAME) reads data from text file FILENAME for the
% default selection. Returns the data as a table.
%
% MS1 = IMPORTFILE(FILE, DATALINES) reads data for the specified row
% interval(s) of text file FILENAME. Specify DATALINES as a positive
% scalar integer or a N-by-2 array of positive scalar integers for
% dis-contiguous row intervals.
%
% Example:
% MS1 = importfile("C:\Users\A0H36019\Documents\MS1.csv", [1, Inf]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 26-Apr-2022 14:43:08
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [1, Inf];
end
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = ["\t", " "];
% Specify column names and types
opts.VariableNames = ["VarName1", "VarName2", "VarName3"];
opts.VariableTypes = ["datetime", "string", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
% Specify variable properties
opts = setvaropts(opts, "VarName2", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "VarName2", "EmptyFieldRule", "auto");
opts = setvaropts(opts, "VarName1", "InputFormat", "yyyy-MM-dd");
opts = setvaropts(opts, "VarName3", "TrimNonNumeric", true);
opts = setvaropts(opts, "VarName3", "DecimalSeparator", ",");
opts = setvaropts(opts, "VarName3", "ThousandsSeparator", ".");
% Import the data
T = readtable(filename, opts);
end

추가 답변 (1개)

Johannes Hougaard
Johannes Hougaard 2022년 4월 26일
Another option which requires less of a custom function but still use the 'detectImportOptions' is to specify in your detectImportOptions that the decimal separator is comma
opts = detectImportOptions('MS1.csv','DecimalSeparator',',');
datatable = readtable('MS1.csv',opts);
head(datatable)
ans = 8×3 table
Var1 Var2 Var3 __________ ____________ ____ 2022-04-22 10:40:45.763 24.7 2022-04-22 10:41:23.875 24.4 2022-04-22 10:42:01.898 24.6 2022-04-22 10:42:39.999 24.7 2022-04-22 10:43:18.065 24.6 2022-04-22 10:43:56.145 24.7 2022-04-22 10:44:34.208 24.4 2022-04-22 10:45:12.214 24.7

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by