Code covered by the BSD License  

Highlights from
readline.m v3.0 (Jun, 2009)

3.875

3.9 | 8 ratings Rate this file 21 Downloads (last 30 days) File Size: 4.46 KB File ID: #20026

readline.m v3.0 (Jun, 2009)

by

 

22 May 2008 (Updated )

Reads the specified line(s) from a file.

| Watch this File

File Information
Description

USAGE:

% Reads the first line:
Vs = readline('data.dat');

% Reads the 8 line:
Vs = readline('data.dat',8);
 
% Reads the [1 2 4 8] lines:
Cs = readline('data.dat',[1 2 4 8]);

% Reads from line 8 to 12:
Cs = readline('data.dat',8:12); % or
Cs = readline('data.dat',[8 12]');

% Reads from 7 to end of file:
Cs = readline('data.dat',[7 -1]');

% Reads from 12 to the last but three lines:
Cs = readline('data.dat',[12 -4]');

NOTES:

- Vs output is a single string vector.
- Cs output is a cell array of strings.
- If the user needs a matrix string output, put 1 as a third option:
Ms = readline('data.dat',[12 -4]',1);

DESCRIPTION:

This program reads specified line(s) from an ascii file, into a string row vector or (if several lines) into a cell array of strings (it can be a matrix string if you force it).

The program tries to read from the first line up to the last specified line, so, it don't has to read the whole file.

FINALLY:

You can save the readed lines with SAVEASCII function as the following example:

saveascii(readline('data1.dat',[3:7],1),'data2.dat')

Acknowledgements

Saveascii.M V6.2 (Oct 2009) inspired this file.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
01 Sep 2011 Simon Musall

Very handy, thanks for the submission

11 Nov 2009 Martin Richard

Well done

22 Apr 2009 Carlos Adrian Vargas Aguilera

Is not true Gabriel, it need to be like that so to be able to generate range from negative inputs. The author.

02 Apr 2009 Gabriel Akira Schreiber

Thank you.

Imprivement:
When called readline(file, lines) and lines is a vector, it should not matter wether lines is a row or column vector.

15 Jul 2008 Peter K. Lord

Very practical and easy to use.

10 Jun 2008 Carlos Adrian Vargas Aguilera

New version ready.

02 Jun 2008 Carlos Adrian Vargas Aguilera

Us is terrible right... the code can be improved to be much more faster and it even has some errors (do not close the file, etc). Please wait to new submition...

28 May 2008 Urs (us) Schwarz

sorry, but for once(!) i strongly and distinctly disagree with mr shvorob and jd: this is NOT the way to do it in a typical 21st century computational environment.
us

28 May 2008 John D'Errico

Sorry, I forgot to add the rating.

28 May 2008 John D'Errico

Yes, I agree with Dimitri. This is not a world class piece of code, in the sense that it solves some problem of Nobel prize importance. This is only a wrapper for some simple calls.

But as Dimitri said, it does what it claims to do. It is well documented. It has an H1 line, proper error messages. I find it a pleasure to see code well done. Maybe I'm just relieved to find something well done in comparison to the rest of the crop uploaded to the file exchange on this day. Regardless, I'll give this a 5 rating. If you need/want something that does what this does, then this code does it well.

27 May 2008 Carlos Adrian Vargas Aguilera

Just for fun, Dimitri (and more clearer writing of course)...

27 May 2008 Dimitri Shvorob

So much code for a fairly straightforward FGETL wrapper? Still, it works, no complaints.

Updates
08 Jun 2009

V3.0 Rewritten code. Negative NLINES values now indicated number of line from the End-of-File, instead of from the last line. New pairwise optional inputs for TEXTSCAN.

Contact us