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

5 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2019-12-10
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 个评论
Guillaume
Guillaume 2019-12-11
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by