read large amount of formatted data
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi!
I have the following problem: From an experiment I get large amounts of data, each equally formatted and put into one huge datafile.
Each set of data has a header, that consists of a number of lines like these:
Measurement
Device Name: lu234
Measurement Name: 1a1
Date: 13.12.2011 12:33
Comment:
Sweep: Channel B
Sweep: b=10.00. e=-60.00. s=-2.00
Delay Time = {0.100000.0.500000.2.000000}
and the data following ...
TransferData
1.000110E+1 -1.999950E+0 7.803810E-10 -4.414860E-10 -2.000000E+0
8.000990E+0 -1.999950E+0 -1.840490E-10 -8.939780E-10 -2.000000E+0
6.000650E+0 -1.999950E+0 5.676950E-10 -2.541150E-11 -2.000000E+0
4.001500E+0 -1.999970E+0 -8.976500E-8 -5.087180E-7 -2.000000E+0
1.999960E+0 -1.999940E+0 -1.073830E-9 -2.164310E-10 -2.000000E+0
...
...
1.000120E+1 -3.999680E+1 1.862440E-8 -5.154440E-9 -4.000000E+1
TransferData End
Measurement End
I started with using textscan to import the data, extracting the Device Name and the Measurement Name and importing the data into an array.
But now I am kind of stuck.. how can I move forward through the datafile? I would need to jump to the next set of data and so on until I reach EOF...
any help is appreciated!
Thank you in advance Chris
0 comentarios
Respuestas (2)
Dr. Seis
el 19 de Dic. de 2011
Looks like a similar problem to the one answered here:
To borrow from his answer:
% Open file and save necessary header info
fid = fopen('data.txt');
foo = fgetl(fid);
device_name = textscan(fid,'Device Name: %s');
measurement_name = textscan(fid,'Measurement Name: %s');
foo = '';
while isempty(strfind(foo,'TransferData'))
foo = fgetl(fid);
end
% Save data from 5-column text file
foo = textscan(fid,'%f%f%f%f%f');
import_data = zeros(size(foo{1},1),5);
for i = 1 : 5
import_data(:,i) = foo{i};
end
fclose(fid);
[EDIT BELOW]
Assuming your file looks like:
Measurement
Device Name: lu234
Measurement Name: 1a1
Date: 13.12.2011 12:33
Comment:
Sweep: Channel B
Sweep: b=10.00. e=-60.00. s=-2.00
Delay Time = {0.100000.0.500000.2.000000}
TransferData
1.000110E+1 -1.999950E+0 7.803810E-10 -4.414860E-10 -2.000000E+0
8.000990E+0 -1.999950E+0 -1.840490E-10 -8.939780E-10 -2.000000E+0
6.000650E+0 -1.999950E+0 5.676950E-10 -2.541150E-11 -2.000000E+0
4.001500E+0 -1.999970E+0 -8.976500E-8 -5.087180E-7 -2.000000E+0
1.999960E+0 -1.999940E+0 -1.073830E-9 -2.164310E-10 -2.000000E+0
1.000120E+1 -3.999680E+1 1.862440E-8 -5.154440E-9 -4.000000E+1
TransferData End
TransferData
1.000110E+1 -1.999950E+0 7.803810E-10 -4.414860E-10 -2.000000E+0
8.000990E+0 -1.999950E+0 -1.840490E-10 -8.939780E-10 -2.000000E+0
6.000650E+0 -1.999950E+0 5.676950E-10 -2.541150E-11 -2.000000E+0
4.001500E+0 -1.999970E+0 -8.976500E-8 -5.087180E-7 -2.000000E+0
1.999960E+0 -1.999940E+0 -1.073830E-9 -2.164310E-10 -2.000000E+0
1.000120E+1 -3.999680E+1 1.862440E-8 -5.154440E-9 -4.000000E+1
TransferData End
Measurement End
Then this should be able to go through each chunk of data. Right now the "import_data" is being written over for each chuck... I will leave it to you to store the data in the way you feel most appropriate.
% Open file and save necessary header info
fid = fopen('matlabtest.txt');
foo = fgetl(fid);
device_name = textscan(fid,'Device Name: %s');
measurement_name = textscan(fid,'Measurement Name: %s');
while ~feof(fid)
foo = '';
while isempty(strfind(foo,'TransferData')) && ...
isempty(strfind(foo,'Measurement End'))
foo = fgetl(fid);
end
if ~isempty(strfind(foo,'Measurement End'))
break;
end
% Save data
foo = textscan(fid,'%f%f%f%f%f');
import_data = zeros(size(foo{1},1),5);
for i = 1 : 5
import_data(:,i) = foo{i};
end
foo = fgetl(fid);
end
fclose(fid);
4 comentarios
Walter Roberson
el 19 de Dic. de 2011
It is not possible to jump forward lines using fseek() . But if you wish you could add 'HeaderLines', 2 to the textscan() options. It happens that it will not matter because textscan() will skip over whitespace (including empty lines) when trying to match a %f format.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!