MATLAB Answers

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

3 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 10 Dec 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 Comments

Show 1 older comment
Michael Käufl
Michael Käufl on 11 Dec 2019
This is my data file (.csv). All I wanna have is the numerical values of each coloumn (but starting at coloumn 20) in a matrix. All values of one coloumn should be in one coloumn and the order of the coloumn must not change - that's important.
Could you give ma a 'route' how get to my target?
Cheers Michael
Guillaume
Guillaume on 11 Dec 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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.