0

I am working on a GUI program that involves uploading a txt file. The first 80 lines of the file contains information I do not need, it is a mix of numeric and text data. For example here is a few lines of the data I do not want to use;

vertical_line_flag;0
vertical_line_ratio;0
laser_wavelength;0
laser_powerlevel;0
overlay_js;0
Relative Intensity Correction Flag;0
Pixel;Wavelength;Wavenumber;Raman Shift;Dark;Reference;Raw data #1;Dark Subtracted #1;%TR #1;Absorbance #1;Irradiance (lumen) #1;
0;165.98;60247.73;-60247.73;0.0000;65535.0000;1125.0000;31.0000;0.0000;0.0000;0.0000; 1;166.38;60103.59;-60103.59;0.0000;65535.0000;549.0000;-545.0000;0.0000;0.0000;0.0000; 2;166.78;59960.14;-59960.14;0.0000;65535.0000;0.0000;-1094.0000;0.0000;0.0000;0.0000; 3;167.18;59817.38;-59817.38;0.0000;65535.0000;998.0000;-96.0000;0.0000;0.0000;0.0000; 4;167.57;59675.31;-59675.31;0.0000;65535.0000;1046.0000;-48.0000;0.0000;0.0000;0.0000; 5;167.97;59533.90;-59533.90;0.0000;65535.0000;1020.0000;-74.0000;0.0000;0.0000;0.0000; 6;168.37;59393.17;-59393.17;0.0000;65535.0000;1003.0000;-91.0000;0.0000;0.0000;0.0000; 7;168.77;59253.11;-59253.11;0.0000;65535.0000;1051.0000;-43.0000;0.0000;0.0000;0.0000; 8;169.17;59113.71;-59113.71;0.0000;65535.0000;1024.0000;-70.0000;0.0000;0.0000;0.0000; 9;169.56;58974.97;-58974.97;0.0000;65535.0000;1045.0000;-49.0000;0.0000;0.0000;0.0000; 10;169.96;58836.88;-58836.88;0.0000;65535.0000;1091.0000;-3.0000;0.0000;0.0000;0.0000; 11;170.36;58699.44;-58699.44;0.0000;65535.0000;1064.0000;-30.0000;0.0000;0.0000;0.0000; 12;170.76;58562.65;-58562.65;0.0000;65535.0000;1019.0000;-75.0000;0.0000;0.0000;0.0000;

The point where the numbers begin, as in the line after (lumen) #1; is the data that I want to use in my program.

I have tried using this code to skip the first 80 lines of the file

[FileName,PathName]= uigetfile('*.txt*','Files to Study');

file =fullfile(PathName,FileName);

 fid = fopen(file);
 A = textscan(fid,'%f' ,'HeaderLines',80);

but this results in A= [0x1 double]

Any suggestions on how to resolve this would be greatly appreciated.

user3580436
  • 47
  • 5
  • 12
  • Are you sure it has more than 80 lines? What happens with a lower number, for example `A= textscan('filename.txt','%f','HeaderLines',4)`? – Luis Mendo Apr 28 '14 at 11:52
  • can you tell what's after these 80 lines? – Acorbe Apr 28 '14 at 11:52
  • Yes its numerica data heres a couple of lines of it: 0;165.98;60247.73;-60247.73;0.0000;65535.0000;1125.0000;31.0000;0.0000;0.0000;0.0000; 1;166.38;60103.59;-60103.59;0.0000;65535.0000;549.0000;-545.0000;0.0000;0.0000;0.0000; 2;166.78;59960.14;-59960.14;0.0000;65535.0000;0.0000;-1094.0000;0.0000;0.0000;0.0000; 3;167.18;59817.38;-59817.38;0.0000;65535.0000;998.0000;-96.0000;0.0000;0.0000;0.0000; 4;167.57;59675.31;-59675.31;0.0000;65535.0000;1046.0000;-48.0000;0.0000;0.0000;0.0000; 5;167.97;59533.90;-59533.90;0.0000;65535.0000;1020.0000;-74.0000;0.0000;0.0000;0.0000; – user3580436 Apr 28 '14 at 13:16
  • Have you checked the doc for textscan? Note Before reading a file with textscan, you must open the file with the fopen function. fopen supplies the fid input required by textscan. When you are finished reading from the file, close the file by calling fclose(fid). – Clemens Apr 28 '14 at 13:22
  • Hi, Luis I'm pretty sure it is 80 lines yes, as in if I open the file in excel the data I want begins at row 81 and when I tried using a lower number it produced the same outcome. – user3580436 Apr 28 '14 at 13:24
  • @user2991246 Yes, sorry I should have that part of my code also included that in my question I use [FileName,PathName]= uigetfile('*.txt*','Files to Study'); %fullfile puts pathname and filename into one file so it can be used to %import data from the chosen file file =fullfile(PathName,FileName); fid = fopen(file); A = textscan(fid,'%f' ,'HeaderLines',80); – user3580436 Apr 28 '14 at 13:29
  • Please edit your question to include the example data and the code that you've put in comments. – Peter Apr 28 '14 at 13:48
  • @Peter I have edited my question, I hope it has made my problem clearer. – user3580436 Apr 28 '14 at 13:56

2 Answers2

2

The problem with your current use of textscan is that ; is not a default delimiter. So this will work:

fid = fopen(file);
A = textscan(fid,'%f' ,'HeaderLines',80,'Delimiter',';');

But if the rest of the file is 100% numerical then you can just use dlmread and set row and column values at which to start reading (note: unlike pretty much everything else in MATLAB, these start at zero).

For your test data:

A = dlmread('test.txt',';',7,0);

For the real data with 80 lines of header:

A = dlmread(filename,';',80,0);
nkjt
  • 7,825
  • 9
  • 22
  • 28
1

Try this code:

A = [];
tline = fgets(fid);
while ischar(tline)
    parts = textscan(tline, '%d;');
    if numel(parts{1}) > 0
        A = [ A ; parts{:}' ];
    end
    tline = fgets(fid);

end

fclose(fid);

Basically, it looks for lines with numerical values in the text file. For each one of these lines, scan the values and attach them as rows in matrix A.

The output I get with your text is:

A =

       0         166       60248      -60248           0       65535        1125          31           0           0           0
       1         166       60104      -60104           0       65535         549        -545           0           0           0
       2         167       59960      -59960           0       65535           0       -1094           0           0           0
       3         167       59817      -59817           0       65535         998         -96           0           0           0
       4         168       59675      -59675           0       65535        1046         -48           0           0           0
       5         168       59534      -59534           0       65535        1020         -74           0           0           0
       ...

Hope that it helps, Regards.

lackadaisical
  • 1,628
  • 18
  • 21