How can I import text and data from a .txt file?

1 Ansicht (letzte 30 Tage)
Gregory Simms
Gregory Simms am 24 Aug. 2015
Kommentiert: Gregory Simms am 24 Aug. 2015
The format of the file is two column, with headers "Masses" and "Intensities"
However, those headers are rewritten after each scan, so the file looks like this:
Masses Intensities
10.0 1.2131233e7
11.0 3.13214e5
12.0 2.234234e7
Masses Intensities
10.0 2.2314324e7
11.0 4.12123123e5
12.0 1.921393e7
Masses Intensities
...
and so on.
When I use a command like
A = importdata('C:\Users\gps9h\Dropbox\MBMS\data\2015_8_24\2015_8_24_Run_2_chro.txt','\t'); % code
I get a structure A which has all of the data from the first scan, and also has the headers, which is good. But how do I get it to extract the data from all of the scans? (ie not to stop after it comes across the words 'Masses' and 'Intensities' for the second time?
I played around with fscanf and load commands and nothing was doing quite what I wanted. The IDEAL would be to make a 3-d array where I have scans 1-N, and then a column of masses and a column of intensities for each scan.
Thanks so much! This is my first time using matlab forum so let me know if anything is out of line in how I ask the question or if this has been answered elsewhere.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Aug. 2015
Bearbeitet: Walter Roberson am 24 Aug. 2015
vals_per_scan = 3;
fid = fopen('C:\Users\gps9h\Dropbox\MBMS\data\2015_8_24\2015_8_24_Run_2_chro.txt', 'rt');
datacell = textscan(fid, '%f%f', 'CollectOutput', 1, 'Delimiter', '\t', 'CommentStyle', 'Masses');
fclose(fid);
data = permute( reshape(datacell{1}, vals_per_scan, [], 2), [1 3 2]);
This will be a 3D array, vals_per_scan (3) by 2 by the number of scans. If you want the scan number to be the first index, use [2 1 3] instead of [1 3 2]. When deciding which index you want first, please take into account that consecutive locations correspond to changing the first index, so A(2,1) is the item next in memory after A(1,1). If you will be processing scan by scan then it is more efficient to have the scan number last, but if you are processing across scans (e.g., plotting how the first mass changes over the scans) then you should have the fastest changing index first.
This depends upon the number of values per scan always being the same.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by