Code covered by the BSD License  

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

3.875

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

readline.m v3.0 (Jun, 2009)

by Carlos Adrian Vargas Aguilera

 

22 May 2008 (Updated 08 Jun 2009)

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

The author wishes to acknowledge the following in the creation of this submission:
saveascii.m v6.2 (Oct 2009)

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
27 May 2008 Dimitri Shvorob

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

27 May 2008 Carlos Adrian Vargas Aguilera

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

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.

28 May 2008 John D'Errico

Sorry, I forgot to add the rating.

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

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...

10 Jun 2008 Carlos Adrian Vargas Aguilera

New version ready.

15 Jul 2008 Peter K. Lord

Very practical and easy to use.

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.

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.

11 Nov 2009 Martin Richard

Well done

01 Sep 2011 Simon Musall

Very handy, thanks for the submission

Please login to add a comment or rating.
Updates
10 Jun 2008

1. I change the loops of FGETL by a single TEXTREAD function as suggested by Urs (us) Schwarz.

2. New easy way for the lines inputs. Now accepts ascending and descending (negative) ranges.

3. I eliminate the file identifier input.

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.

Tag Activity for this File
Tag Applied By Date/Time
data import Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
data export Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
getl Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
fopen Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
ascii Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
read Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
text Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
string Carlos Adrian Vargas Aguilera 22 Oct 2008 10:03:00
textread Carlos Adrian Vargas Aguilera 08 Jun 2009 15:40:34
textscan Carlos Adrian Vargas Aguilera 08 Jun 2009 15:40:34
dlmread Carlos Adrian Vargas Aguilera 08 Jun 2009 15:40:34
load Carlos Adrian Vargas Aguilera 08 Jun 2009 15:40:34
textscan Luis C. 30 Jun 2009 16:35:56
getl mahmoud kaseb 10 Mar 2010 18:56:07

Contact us at files@mathworks.com