reading a .txt

조회 수: 4 (최근 30일)
nono
nono 2016년 10월 11일
편집: Thorsten 2016년 10월 20일
Dear Everyone, I am really embarrased, and I m pretty sure my problem is simple... I have a txt file with 2 columns of float numbers seperated with a tabulation. Above and below those two columns there are informations that might be interesting. First of all I d like to proceed to the acquisition of the 2 columns, ONLY. Secondly I'd like to get the value of some other informations like "resolution" (at the end of the file) value which is 20.00 here. Please find attached the text file. Best regards and thank you very much for your help :-)
  댓글 수: 2
dpb
dpb 2016년 10월 11일
Extra blank lines actually in the file or are they a figment of the saving the file? Need to know that in order to be able to count the headerlines.
Is the length varying, I presume?
In general, once the above issue is resolved, reading the numeric data is pretty-much trivial, dlmread will do just fine.
To read both sections, probably switch to textscan; read the numeric data and then you can use fgetl to read records to find the string "resolution" and then read the values from it.
There are enough examples in the help files should give a pretty good idea how to start but first need to know about the extra newlines--yes or no?
Walter Roberson
Walter Roberson 2016년 10월 11일
Technically that file is a binary file. There are no empty lines in the file, but each line ends in \r\r\n . Text files can have their lines ending in \r\n or \n but not \r\r\n .
That said, if you fopen() with 'rt' permissions, then MATLAB will treat the \r\r\n as a single end-of-line.

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

답변 (2개)

Jean-Marie Sainthillier
Jean-Marie Sainthillier 2016년 10월 20일
편집: Jean-Marie Sainthillier 2016년 10월 20일
Try something like:
function M=read_X()
fid=fopen('01.txt');
fgetl(fid); % YOKO
fgetl(fid); % START
M=[];
temp=fgetl(fid);
while ~isequal(temp,'STOP')
temp=fgetl(fid);
if ~isempty(temp)
M=[M ;str2num(temp)];
end
end
fclose(fid);

Thorsten
Thorsten 2016년 10월 20일
편집: Thorsten 2016년 10월 20일
First check if the encoding is ISO-8859-1, such that accents are read correctly:
if ~strcmp(feature('DefaultCharacterSet'), 'ISO-8859-1')
feature('DefaultCharacterSet', 'ISO-8859-1')
end
X = textread('01.txt', '%s');
ind = find(strcmp(X, 'START')):find(strcmp(X, 'STOP'));
ind = ind(2:end-1); % numbers between START and STOP
Data = cellfun(@(x) sscanf(x, '%f'), X(ind));
Data = reshape(Data, 2, [])';
ind = find(strcmp(X, 'Résolution')) + 2;
resolution = sscanf(X{ind}, '%f');

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by