Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
File extraction ignoring all info until a specific 'string' is found

Subject: File extraction ignoring all info until a specific 'string' is found

From: Josh

Date: 11 Aug, 2011 18:44:13

Message: 1 of 3

Hi Everyone,

     I would like to get data from a text file. The problem I have is that the file contains any number of header lines before the data begins. Fortunately there is always a line with two astericks (**) before the actual data. As such, I would like to open the file, scan line by line until I find the asterics (or any other string that may seperate the header from the desired data), and then load all further lines of data according to their particular format using something akin to textread. Here is an example of what the text file may look like:

--------------HEADER-------------------
Miscellaneous header info
May contain things like 'This_3'
AND CAN: Be any length

Blank Lines may also exist ^
DATE: 8/11/2011 11:40:54 AM
EvenEquationsArelikely = 1.47637795275591E-02
--------------MORE INFO---------------
PA - plot absolute
PD - Pen Down
AA - Plot arc in absolute
PU is pen up
-------------------------------------------
**
PA -1.5704,-10.9749;PD;AA -39.1348, 0.000, 32.5729,0.004;PU;
PA -1.5636, 11.0185;PD;AA -39.1348, 0.000,-32.6898,0.004;PU;
PA -1.5564,-11.0605;PD;AA -39.1348, 0.000, 32.8018,0.004;PU;
PA -1.5488, 11.1010;PD;AA -39.1348, 0.000,-32.9091,0.004;PU;
PA -1.5408,-11.1401;PD;AA -39.1348, 0.000, 33.0120,0.004;PU;
PA -1.5325, 11.1778;PD;AA -39.1348, 0.000,-33.1107,0.004;PU;
PA -1.5237,-11.2142;PD;AA -39.1348, 0.000, 33.2052,0.004;PU;


So in this case, I am interested in gathering all of the data that starts wtih PA. (For some this may look familiar, it is HPGL machine code, and basically I am looking to read in this code, and create a plot showing the pattern that this code specifies as a preview)

Any help would be most appreciated.

Thanks,
    Josh

Subject: File extraction ignoring all info until a specific 'string' is

From: dpb

Date: 11 Aug, 2011 21:04:28

Message: 2 of 3

On 8/11/2011 1:44 PM, Josh wrote:
> Hi Everyone,
>
> I would like to get data from a text file. The problem I have is that
> the file contains any number of header lines before the data begins.
> Fortunately there is always a line with two astericks (**) before the
> actual data. As such, I would like to open the file, scan line by line
> until I find the asterics (or any other string that may seperate the
> header from the desired data),...

fid=fopen(yourfile,'rt');
found=false;
while ~found
   l=fgetl(fid);
   found=findstr(l,'**')==1
end
% carry on reading data from here...

--

Subject: File extraction ignoring all info until a specific 'string' is found

From: someone

Date: 11 Aug, 2011 21:17:28

Message: 3 of 3

"Josh " <joshinbox@hotmail.com> wrote in message <j217tt$jd$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> I would like to get data from a text file. The problem I have is that the file contains any number of header lines before the data begins. Fortunately there is always a line with two astericks (**) before the actual data. As such, I would like to open the file, scan line by line until I find the asterics (or any other string that may seperate the header from the desired data), and then load all further lines of data according to their particular format using something akin to textread. Here is an example of what the text file may look like:
>
> --------------HEADER-------------------
> Miscellaneous header info
> May contain things like 'This_3'
> AND CAN: Be any length
>
> Blank Lines may also exist ^
> DATE: 8/11/2011 11:40:54 AM
> EvenEquationsArelikely = 1.47637795275591E-02
> --------------MORE INFO---------------
> PA - plot absolute
> PD - Pen Down
> AA - Plot arc in absolute
> PU is pen up
> -------------------------------------------
> **
> PA -1.5704,-10.9749;PD;AA -39.1348, 0.000, 32.5729,0.004;PU;
> PA -1.5636, 11.0185;PD;AA -39.1348, 0.000,-32.6898,0.004;PU;
> PA -1.5564,-11.0605;PD;AA -39.1348, 0.000, 32.8018,0.004;PU;
> PA -1.5488, 11.1010;PD;AA -39.1348, 0.000,-32.9091,0.004;PU;
> PA -1.5408,-11.1401;PD;AA -39.1348, 0.000, 33.0120,0.004;PU;
> PA -1.5325, 11.1778;PD;AA -39.1348, 0.000,-33.1107,0.004;PU;
> PA -1.5237,-11.2142;PD;AA -39.1348, 0.000, 33.2052,0.004;PU;
>
>
> So in this case, I am interested in gathering all of the data that starts wtih PA. (For some this may look familiar, it is HPGL machine code, and basically I am looking to read in this code, and create a plot showing the pattern that this code specifies as a preview)
>
> Any help would be most appreciated.
>
> Thanks,
> Josh

doc fgetl

% Try something like (to get you started):

fid=fopen('yourfile.txt');
tline = '//'
while tline(1:2) ~= '**'
    tline = fgetl(fid);
end

tline = fgetl(fid); % This is the fisrt line of 'good" data
disp(tline)
% insert code (using fgetl) to read rest of your data here

fclose(fid);

% There are bugs in the above code,
% but that should get you started.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us