Read the third column of a .csv file
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alex Perrakis
el 26 de Abr. de 2022
Respondida: Johannes Hougaard
el 26 de Abr. de 2022
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 comentarios
Respuesta aceptada
Mathieu NOE
el 26 de Abr. de 2022
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
0 comentarios
Más respuestas (1)
Johannes Hougaard
el 26 de Abr. de 2022
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)
0 comentarios
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!