From help-request at octave dot org Tue Jun 8 11:43:29 2004 Subject: Re: Plotting data from a file containing comments as first lines From: Jonathan Stickel To: help at octave dot org Date: Tue, 08 Jun 2004 09:17:51 -0700 vincent dot marembert at free dot fr wrote: > Hi, > > my problem: > I would like to plot the data contained in a file beginning with comments... > The file looks like: > > blabla1 > blabla2 > dsfsdsdfd > 1 3 > -5 6 > 0 8 > 1 8 > > Of course it would be easy if there was no comments at the beginning, I > would just load the data typing > load data > and then plot it with > plot data > > Is there a way to tell Matlab (or Octave, because I work a lot with Octave) > to ignore the first 3 or 4 lines ? There must be one, but I can t find > it... > For this, I think you must use the low-level fopen, fscanf type commands. Pasted below is a function I just wrote that I think will satisfy your needs. Jonathan function data=datareadwh(filename,nhl,nc) %%data=datareadwh(filename,nhl,nc) %% Read data from a file, skipping the first nhl lines %% (header lines). nc is the number of columns the data %% is expected to be in. %% author: Jonathan Stickel %% created: 8 June 2004 %% modified: 8 June 2004 [fid,msg] = fopen (filename, "r"); for i=1:nhl fgetl(fid); endfor [data,count] = fscanf (fid, "%f"); fclose(fid); nr=count/nc; if ( nr-floor(nr) ) disp("warning: data read is not an integer multiple of the specified number of columns;") disp("returning data in a single column") else data=reshape (data,nc,nr)'; endif endfunction ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------