Thread Subject: Using textscan to read in data

Subject: Using textscan to read in data

From: Felipe Sediles

Date: 24 Jul, 2007 21:58:45

Message: 1 of 3

Hi, I have data in a text file I'd like to read using textscan. The data is arranged as follows:

 1, 1, 2, 566, 619, 8, 5, 646, 647, 21445, 21444, 21443, 21442, 21446, 21447, 21448,
    21449, 21451, 21450, 21452, 21453
 2, 619, 566, 567, 618, 647, 646, 645, 648, 21443, 21456, 21455, 21454, 21448, 21457, 21458,
    21459, 21453, 21452, 21460, 21461
 3, 618, 567, 568, 617, 648, 645, 644, 649, 21455, 21464, 21463, 21462, 21458, 21465, 21466,
    21467, 21461, 21460, 21468, 21469
 4, 617, 568, 569, 616, 649, 644, 643, 650, 21463, 21472, 21471, 21470, 21466, 21473, 21474,
    21475, 21469, 21468, 21476, 21477
 5, 616, 569, 570, 615, 650, 643, 642, 651, 21471, 21480, 21479, 21478, 21474, 21481, 21482,
    21483, 21477, 21476, 21484, 21485

I want to read this as 21 columns. In my text file there is one carriage return after the first 16 integers in a row, then 5 more integers in another row. So each actual row contains 21 integer entries. In what I've pasted there are 5 rows of data. I'm having a problem reading this in. I've been trying:

ELEMENTS=textscan(fid,'%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, \n %d, %d, %d, %d, %d\n');

but that hasn't been working. Any help will be greatly appreciated.

Felipe

Subject: Using textscan to read in data

From: us

Date: 25 Jul, 2007 01:50:36

Message: 2 of 3

Felipe Sediles:
<SNIP how to read a formatted text file - evergreen

one of the many solutions

% the engine
     nc=21; % <- # cols
     fnam='num21.txt'; % <- your file name!
     n=textread(fnam,'%d','delimiter',',');
     m=reshape(n.',nc,[]).';
% the result
     type(fnam);
     m

us

Subject: Using textscan to read in data

From: Felipe Sediles

Date: 25 Jul, 2007 02:42:47

Message: 3 of 3

"us " <us@neurol.unizh.ch> wrote in message <f86a9c$jld$1@fred.mathworks.com>...
> Felipe Sediles:
> <SNIP how to read a formatted text file - evergreen
>
> one of the many solutions
>
> % the engine
> nc=21; % <- # cols
> fnam='num21.txt'; % <- your file name!
> n=textread(fnam,'%d','delimiter',',');
> m=reshape(n.',nc,[]).';
> % the result
> type(fnam);
> m
>
> us
>
Thank you that worked!

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
evergreen us 30 Jan, 2008 20:26:04
textread us 24 Jul, 2007 21:55:06
code us 24 Jul, 2007 21:55:06
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