Thread Subject: read data in Matlab

Subject: read data in Matlab

From: Jerry

Date: 29 Aug, 2008 16:58:01

Message: 1 of 2

Suppose I have a data file:

ID C1 C2 ... C1000
R1 # # #
R2 # # #
.
.
.
R2000 # # #

How can I read it into three variables: ColID, RowID, and
1 2000x1000 matrix of numerical values? textscan or
textread seems to need a format specifier for each column
which is troublesome for large number of columns.

Also if the file is too big and I only need to read in
certain rows (for example, only read in R2, R28, R34,...),
then how can I do this?

Any solution or hints? Thanks very much.

Subject: read data in Matlab

From: Walter Roberson

Date: 29 Aug, 2008 17:40:16

Message: 2 of 2

Jerry wrote:
> Suppose I have a data file:
>
> ID C1 C2 ... C1000
> R1 # # #
> R2 # # #
> .
> .
> .
> R2000 # # #

> How can I read it into three variables: ColID, RowID, and
> 1 2000x1000 matrix of numerical values? textscan or
> textread seems to need a format specifier for each column
> which is troublesome for large number of columns.

Compute the format instead of writing it by hand.

hfmt = ['%*s', repmat('%s', 1, 1000), '\n'];
nfmt = ['%s', repmat('%f', 1, 1000), '\n'];

Then textscan(fid, hfmt, 1, 'CollectOutput', 1)
to read the column headers.
textscan(fid, nfmt, 'CollectOutput', 1)
to read the row headers and numeric values

> Also if the file is too big and I only need to read in
> certain rows (for example, only read in R2, R28, R34,...),
> then how can I do this?

textscan() one row at a time, with 'Headerlines' set to the number of
rows to skip -- e.g., Headerlines 25 after reading R2. You will
need to do your own work to store each line result -- that is, there
is no facility to say "read only these specified lines and put
everything read into appropriate cell arrays"

--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com