필터 지우기
필터 지우기

Problem in importing textfiles

조회 수: 1 (최근 30일)
afrya
afrya 2015년 1월 9일
댓글: afrya 2015년 1월 12일
Hello,
I would like to import a textfile with matlab. The textfile consist of decimal numbers. It looks like:
X Y Z
0,1 0,2 0,3
0,4 0,5 0,6
0,7 0,8 0,9
I use the following code to import the data:
filename = 'Textfile-test.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
My problem is that the numbers are not imported by matlab. Do you have any idea how to solve this problem.
Thank you in advance

채택된 답변

Stephen23
Stephen23 2015년 1월 11일
MATLAB only recognizes the period character as the decimal radix, so you will need to change that comma character before converting to numeric. To achieve this when reading the file with MATLAB, you could do something like this:
fid = fopen('temp.txt');
C = textscan(fid, '%s%s%s');
fclose(fid);
C = [C{:}];
hdr = C(1,:);
dat = regexprep(C(2:end,:),',','.');
dat = cellfun(@(s)sscanf(s,'%f'),dat);
  댓글 수: 1
afrya
afrya 2015년 1월 12일
Thank you for your answer, it works now

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

추가 답변 (1개)

dpb
dpb 2015년 1월 9일
What error(s), if any did you get?
With that file structure, simply
x=importdata(filename);
should work altho don't see anything obviously wrong with the specific form.
Have you ensured the file is actually on the Matlab search path and the filename is spelled correctly, etc., etc, etc., ...?
  댓글 수: 3
dpb
dpb 2015년 1월 11일
...the decimal limiter is the comma...
Woops, my old eyes whiffed on that, sorry. AFAIK, that's not a changeable option, you either have to change the file format itself or read the data and do a translation in memory before scanning the data.
It's a pain in the proverbial appendage for those locales where the convention is the other way 'round, I know...
afrya
afrya 2015년 1월 12일
편집: afrya 2015년 1월 12일
Ok thank you for your help

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

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by