reading a .txt
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 11 de Oct. de 2016
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
el 11 de Oct. de 2016
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.
Respuestas (2)
Jean-Marie Sainthillier
el 20 de Oct. de 2016
Editada: Jean-Marie Sainthillier
el 20 de Oct. de 2016
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);
0 comentarios
Thorsten
el 20 de Oct. de 2016
Editada: Thorsten
el 20 de Oct. de 2016
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');
0 comentarios
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!