Problem reading a csv file

조회 수: 5 (최근 30일)
Erik Börjesson
Erik Börjesson 2019년 2월 3일
댓글: Satoshi Kobayashi 2019년 2월 4일
Hello,
I am having problem reading in this file to matlab.
filename = 'Aluminum3.2_RawData_1.csv';
deliminator = ',';
Matvarden = dlmread(filename, deliminator,[ 50 0 10000 4]);
I want to get the all the numeric data from the file, trying to use dlmread. Here just as a test i tried row 50 --> 10000.
But just keep on getting "Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==> ;0,00;0,00000;-340,00\n"
And I just dont get it, there should only be Numeric values from row 50--> 10 000.
Does anyone have a solution to this?

답변 (2개)

Jeremy Hughes
Jeremy Hughes 2019년 2월 3일
Your CSV is a semicolon delimited file with comma as the decimal separator character.
This ought to work
opts = detectImportoptions(filename,'Delimiter',';')
opts = setvartype(opts,'double');
opts = setvaropts(opts,'DecimalSeparator',',');
opts.DataLines = [50 10000]; % if you want just those rows.
T = readtable(filename,opts);
And if you want a matrix
A = T.Variables

Satoshi Kobayashi
Satoshi Kobayashi 2019년 2월 3일
Your csv file includes semicolons in the range.
Dlmread cannot use two deliminators.
I recommend you to use textscan.
fileID = fopen(filename);
c = textscan(fileID,'%s','EndOfLine','\r\n','Delimiter',{',',';'},'HeaderLines',26);
fclose(fileID);
C = reshape(c{1},8,[])';
M = str2double(C);
  댓글 수: 3
Erik Börjesson
Erik Börjesson 2019년 2월 3일
if i use '/t' instead everything jumps in the same column.. So then i need to get them into four diffrent columns..
c = textscan(fileID,'%s','EndOfLine','\r\n','Delimiter',{'\t',''},'HeaderLines',26);
does someone have an idea on how? If i put
';' in delimiter
Everythings just gets really messy and there is no order.
Satoshi Kobayashi
Satoshi Kobayashi 2019년 2월 4일
If the total number of elements is 156318, the data is not regular.
You can get whole data as cells. You can get any infomation from it.
fileID = fopen(filename);
C01=textscan(fileID,'%s','EndOfLine','\r\n');
fclose(fileID);
for p=1:length(C01{1})
C02=textscan(C01{1}{p},'%s','Delimiter',{',',';'});
C(p,1:length(C02{1}))=C02{1}';
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by