필터 지우기
필터 지우기

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

조회 수: 2 (최근 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