Filter löschen
Filter löschen

How to import csv-file with headlines (type text) to matrix? - only numbers are needed

2 Ansichten (letzte 30 Tage)
Hello community,
I've got a csv-file with first row as headline (type text). But I want to write a script, so the import tool cannot help. I've tried 'importData', csvread (but it supports sheets with numbers only) and textscan. It would be the best when i could choose the range - I only need the numbers.
My csv-file has the following composition:
# | date | time | temperature 1 | temperature 2 | ..... | temperature 732
1 31-10-19 12:31:14 14,2342 15,1234 ....
2 31-10-19 12:31:15 16,4442 15,6283 ....
3 31-10-19 12:31:16 14,8552 15,8294 ....
I only need the temperature values, therefore i want to choose the range. Or is there anothers opportunity to import the numerical values?
Thanks for your help!
Michael

Akzeptierte Antwort

Guillaume
Guillaume am 10 Dez. 2019
Both readtable and readmatrix will handle this file without issue. I would have thought that importdata would delegate to readtable so I'm surprise it didn't work for you.
readtable will label the columns according to the header.
  4 Kommentare
Guillaume
Guillaume am 11 Dez. 2019
If all you want is a matrix from column 20, simply extract that from the table or matrix:
data = readtable('messung2.csv');
selecteddata = data{:, 20:1492}; %extract matrix from table. All selected columns must be numeric
%or
data = readmatrix('messung2.csv'); %non numeric columns in the file are NaN.
selecteddata = data(:, 20:1492);
However, it seems a waste to discard the rest of the columns, in particular the date and time and to discard the column names, so you may want to actually give hints to matlab so it can parse your file properly:
opts = detectImportOptions('messung2.csv', 'VariableNamesLine', 1, 'ExtraColumnsRule', 'ignore');
data = readtable('messung2.csv', opts); %Now it parses the file properly except for the date
data.Datum = datetime(data.Datum, 'InputFormat', 'dd,MM,yyyy'); %so parse the data
%and optionally merge date and time
data.Datum = data.Datum + data.Uhrzeit;
This table, which you could even convert to a timetable, would be very useful to perform aggregate calculation per day/month/year and make it easier to select columns based on name rather than assumed column number.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Standard File Formats finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by