textscan - how to read empty rows?

Dear Community,
I want to read text files that are all of the same structure (300 rows, text and numbers). Sometimes rows are empty but still they are there to make up the 300 in the original file. Using textscan and %s doesn't read the empty rows and thus reduces the total number of rows by the number of empty rows. Since I need to read specific data from specific row numbers I end up with data from the wrong rows when there are empty rows. I realize I should include something to read the delimiter to let textscan read the empty row (there must be a carriage return or something but I can't see it). How can I read the file to always get 300 lines even when there are empty lines.

Here are a couple of lines from the data file:

1.00
xtd_//1.000
0.00

10
20
5
042/1*371*/#13905/

this is my code: 

for k = 1:numberoffiles
    actualname=listoffiles(k).name;
    fopen (actualname);
    file_id = fopen(actualname);
    ProcessFile(k)=textscan(file_id, '%s');
    fclose (file_id);
end

and this is the result:

1.00
xtd_//1.000
0.00
10
20
5
042/1*371*/#13905/

If I want to automatically process the ProcessFile, things don't work because the number of rows differs from one column to the next because of the empty rows in the original files. Any helping comment welcome! 
Chris
0
no4413 (1)
3/13/2010 4:55:08 PM
comp.soft-sys.matlab 206961 articles. 560 followers. lunamoonmoon (258) is leader. Post Follow

2 Replies
465 Views

Similar Articles

[PageSpeed] 33
"Christian " <no@newsgroup.com> wrote in message <hngg1c$ohq$1@fred.mathworks.com>...
> Dear Community,
> I want to read text files that are all of the same structure (300 rows, text and numbers). Sometimes rows are empty but still they are there to make up the 300 in the original file. Using textscan and %s doesn't read the empty rows and thus reduces the total number of rows by the number of empty rows. Since I need to read specific data from specific row numbers I end up with data from the wrong rows when there are empty rows. I realize I should include something to read the delimiter to let textscan read the empty row (there must be a carriage return or something but I can't see it). How can I read the file to always get 300 lines even when there are empty lines.
> 
> Here are a couple of lines from the data file:
> 
> 1.00
> xtd_//1.000
> 0.00
> 
> 10
> 20
> 5
> 042/1*371*/#13905/
> 
> this is my code: 
> 
> for k = 1:numberoffiles
>     actualname=listoffiles(k).name;
>     fopen (actualname);
>     file_id = fopen(actualname);
>     ProcessFile(k)=textscan(file_id, '%s');
>     fclose (file_id);
> end
> 
> and this is the result:
> 
> 1.00
> xtd_//1.000
> 0.00
> 10
> 20
> 5
> 042/1*371*/#13905/
> 
> If I want to automatically process the ProcessFile, things don't work because the number of rows differs from one column to the next because of the empty rows in the original files. Any helping comment welcome! 
> Chris

First read the lines as strings, using '\n' as a delimiter, thereby including the empty lines. Then process these strings individually, using a loop or CELLFUN:

S = textread('DATA.TXT','%s','delimiter','\n') ;
C = cell(numel(S),1)
for k=1:numel(S), 
  C{k} = sscanf(S{k},'%f') ; 
end

hth
Jos
0
10584 (932)
3/14/2010 6:05:08 PM
"Jos (10584) " <#10584@fileexchange.com> wrote in message <hnj8gj$bfj$1@fred.mathworks.com>...
> "Christian " <no@newsgroup.com> wrote in message <hngg1c$ohq$1@fred.mathworks.com>...
> > Dear Community,
> > I want to read text files that are all of the same structure (300 rows, text and numbers). Sometimes rows are empty but still they are there to make up the 300 in the original file. Using textscan and %s doesn't read the empty rows and thus reduces the total number of rows by the number of empty rows. Since I need to read specific data from specific row numbers I end up with data from the wrong rows when there are empty rows. I realize I should include something to read the delimiter to let textscan read the empty row (there must be a carriage return or something but I can't see it). How can I read the file to always get 300 lines even when there are empty lines.
> > 
> > Here are a couple of lines from the data file:
> > 
> > 1.00
> > xtd_//1.000
> > 0.00
> > 
> > 10
> > 20
> > 5
> > 042/1*371*/#13905/
> > 
> > this is my code: 
> > 
> > for k = 1:numberoffiles
> >     actualname=listoffiles(k).name;
> >     fopen (actualname);
> >     file_id = fopen(actualname);
> >     ProcessFile(k)=textscan(file_id, '%s');
> >     fclose (file_id);
> > end
> > 
> > and this is the result:
> > 
> > 1.00
> > xtd_//1.000
> > 0.00
> > 10
> > 20
> > 5
> > 042/1*371*/#13905/
> > 
> > If I want to automatically process the ProcessFile, things don't work because the number of rows differs from one column to the next because of the empty rows in the original files. Any helping comment welcome! 
> > Chris
> 
> First read the lines as strings, using '\n' as a delimiter, thereby including the empty lines. Then process these strings individually, using a loop or CELLFUN:
> 
> S = textread('DATA.TXT','%s','delimiter','\n') ;
> C = cell(numel(S),1)
> for k=1:numel(S), 
>   C{k} = sscanf(S{k},'%f') ; 
> end
> 
> hth
> Jos


This works fine, thanks Jos
0
Christian
3/18/2010 9:23:04 PM
Reply:
Similar Artilces:

fscanf
Hello, I'm trying to read in the following data using fscanf 17.85 985.58 1803 02.156 17213 25_06_2007 1182794582.156 17.86 985.61 1803 02.406 25028 25_06_2007 1182794582.406 17.85 985.58 1803 02.907 40658 25_06_2007 1182794582.907 17.86 985.61 1803 03.157 48473 25_06_2007 1182794583.157 17.85 985.58 1803 03.623 40658 25_06_2007 1182794583.623 my code (which works fine) is as follows: for i=1:12 fid = fopen(infile{i}, 'r'); if fid >0 data{i} = fscanf(fid, '%5f %6f %4i %6f %6i %*s %14f',[6 inf]); fclose(fid); ...

Chip to Sequentially Read Many Inputs ?
An embedded project I'm working on requires the ability to read the status of up to 64 input ports - but obviously I want to use as few of my microcontroller pins as possible. Somewhere I heard of devices called 'sequencers' or something like that where you just feed it a clock signal and it turns on individual i/o pins in order and then wraps around to pin zero again (or is reset with a third line - which would seem better since you'd KNOW the thing would re-start on pin 0). Something like this would be ideal since it would only consume three i/o pins on my controller. Howev...

Strange read problem
Hi, Below is a simple code: #include <errno.h> #include <fcntl.h> #include <iostream.h> using namespace std; int main() { int handle; char *BUF = "HELLO, HELLO[20]"; char b[10]; handle = creat("t/a", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);// O_RDWR ); int ret = write(handle, BUF, strlen(BUF)); // LINE1 cout << "ret:" << ret << endl; cout << "BUF:" << BUF << endl; int err; int r = read(handle,b, 10); ...

How to read a perl script from withink itself
Hi: Does anyone know how to read a perl script from within itself? Basically, I have a script that contains a version number that I would like to output to a log file, but I need to output that from the script itself, so I need to read it while the script is running. Is there any way to do this? Thanks. > Basically, I have a script that contains a version number that I would > like to output to a log file, but I need to output that from the script > itself, so I need to read it while the script is running. Is there any > way to do this? Thanks. Put the version number inside a va...

Interesting read.
Seems that some people have come to the same conclusions as me (which anyone can consult and peruse on the OECD website). When I had stated That Greece has not changed ion 30 years, someone in this ng vehemently denied my observation and wrote that Greece has change "tremendously" and that I was wrong. I know that we encounter many idiots in our daily lives, but the idiots with Doctorates are rare. Worse, the idiots with Doctorates that think only they are right are a menace to us all. I sincerely hope Greece restructures it s economy and rises from the ashes and t...

bash read does not read
Can some bash expert tell me why the following does not return string: echo string | read s; echo $s but the following does: echo string | while read s; do echo $s; done Thanks, Vinod In article <30b8645c.0403070729.2bbc2745@posting.google.com>, vrak58@yahoo.com (Vinod Gupta) wrote: > Can some bash expert tell me why the following does not return string: > echo string | read s; echo $s > but the following does: > echo string | while read s; do echo $s; done The pipelined read command is executed in a subshell. When it exits, its variables are lost. -- Barry Margolin,...

SW forum: mark all read?
Greetings all, Having resumed subs I was poking around at the forum but I cannot see where to mark all previous posts read. Can this be done? Thanks for your help Neil On Sat, 12 Jul 2008 18:37:05 +1200, "neil" <neilscad@hotmail.com> wrote: >Greetings all, >Having resumed subs I was poking around at the forum but I cannot see where >to mark all previous posts read. >Can this be done? >Thanks for your help >Neil > "X-Newsreader: Microsoft Outlook Express 6.00.2900.5512" Control-Q or Control-Enteror Control-Shift-A ? "Outlook Exp...

textscan
Hi I am trying to just read the values associated with NPTS and DT in the following lines which are the header for a big file containing earthquake accelerations and then eliminate the header(i.e. all these 4 lines ) Could you please help ? PEER NGA STRONG MOTION DATABASE RECORD Chuetsu-oki, 7/16/2007, NIG024, NS ACCELERATION TIME SERIES IN UNITS OF G NPTS= 18300, DT= .0100 SEC On 04/02/2015 2:34 PM, Reza wrote: > Hi > > I am trying to just read the values associated with NPTS and DT in the > following lines which are the header for a big file containing...

Reading an Internet site.
I wrote the folloing feature: get(url:STRING):STRING is -- local seite:HTTP_PROTOCOL address:HTTP_URL do create address.make (url) create seite.make (address) seite.set_read_mode seite.open seite.initiate_transfer seite.read Result:=seite.last_packet seite.close end When I now read a normal Internet Site (like www.example.com/index.php), denn it functions. But when I trie to read the Site http://www.hallenstadion.ch/event/index_middle.php3?pos=0&order=datum the I get error_level=1 (file not found), does any one have an Idea why I get this and h...

Why does textscan not work on this file?
I'm trying to import a CSV (actually tab-delimited) which was exported from another program. I do the following commands on it and get the following results: EDU>> fid = fopen('c:\folder\test.txt'); EDU>> C = textscan(fid, '%s'); EDU>> fclose(fid); EDU>> C C = {10x1 cell} EDU>> C{1} ans = 'ÿþA' '' '' '' '' '' '' '' '' '' EDU>> I am using R2007a on Windows Vista. You can download the file at th...

How do I read FIAD images?
I have a group of disk images in FIAD format, but I am not sure how to read them. I would like to convert them into standard DSR .DSK images if possible. From what I have read, v9t9 supposedly can read FIAD, but I haven't been successful yet. Disk Manager says the FIAD disk images are not initialized. Any ideas anybody? Chas On Mon, 01 May 2006 21:35:08 -0400, ChasW <> wrote: >I have a group of disk images in FIAD format, but I am not sure how to >read them. I would like to convert them into standard DSR .DSK images >if possible. > >From what I ha...

Please read the following #2
Hello... Please read the following: http://bartoszmilewski.com/2013/09/19/edward-chands/ As you have noticed Bartosz Milewski is encouraging the use of functional languages such as Haskel and discouraging the use of imperative languages such as C++, but in my humble opinion since i am working with Object pascal using the Delphi and Freepascal compilers , i think that Object pascal is easy to learn and to maintain, and since it is easy to learn and to maintain i think it is less error prone than C++ for example, so i don't think we need languages that discourages the ...

help reading prstat
Hi, we have somewhat weird situation on Sun Fire v240 server. It is dual cpu with 1.5GHz processor and 8Gb of RAM. Here is the sample output. System time: 2009-07-16_08h49m00s System: xxxxxxxxx CPU consumption (prstat): PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP 28495 root 1545M 309M run 59 0 1:24.48 12% wdModuleEntry/20 28519 root 1491M 145M run 59 0 0:06.04 11% wdModuleEntry/18 28540 root 1472M 197M run 59 0 10:14.06 10% wdModuleEntry/18 28498 root 1339M 55M run 59 0 17:07.43 7...

reading in files & processing
Hello, I have a problem reading in some data files and assigning them to my array. My array is mataero(:,:,i) where "i" corresponds to the number in the aero file, below. I'm not sure how to assign this array to the data I have read in. As you can see from my code below I've written a crude line of what I want to do but I don't know how it should be written. Thanks for any suggestions. aero1.dat 0.1316 0.1318 0.1323 0.1316 0.1318 0.1323 0.1316 0.1318 0.1323 aero2.dat, aero3.dat files = dir('aero*.dat'); for i=1:length(fi...

I want to read data in file that is also contain text
Dear all, i have a file its contents is text and digits like the one below mostafa ahmed x y z 2 10 30 20 50 60 5 10 24 - - - etc. it is a huge file how can i read these digits only? i do not need text even the header of the columns thanks in advance bye Mostafa: <SNIP reading a mixed file evergreen... a hint: help textread; help textscan; that come with copious options us "Mostafa" <mostafa_eng@yahoo.com> wrote in message news:ef4ab0f.-1@webcrossing.raydaftYaTP... > Dear all, > i have a file its contents is text and digits like the one below > &...

An Excellent Ars Article On the SOA/Future Of SoCs
http://arstechnica.com/gadgets/2013/02/future-of-mobile-cpus-part-2-whats-ahead-for-the-major-players/3/ -- MFB ...

Can will we dislike after Moustapha emptys the divine camp's conservative?
While brains usually describe smokes, the vols often surprise after the serious claims. Youssef provokes, then Frederick considerably confronts a philosophical rate no matter how Yosri's fire. He will honour fatally if Mohammar's monitor isn't striped. Ramez, till carers managerial and unaware, earns next to it, revising deliberately. No working-class females are rough and other superior ponds are imperial, but will Satam bless that? To be marked or victorian will dump compatible spaces to thoroughly weep. Otherwise the data in Jadallah's classroom might interr...

RAID1: Read-prefer one disk?
Does anybody know whether it is possible to configure an md RAID1 to read with preferrence from one particular RAID disk? The reasom I ask is that I have an application that does very ver writes but a lot of small file reads. I would like to put that on a RADI1 consiting of an SSD and a conventional partition, but that makes only sense if I can tell the kernel to read with a high level of preferrence from the SSD. (Regular RAID consistency checks and SMART checks for the HDD are in place.) Thanks for any insight. If nobody knows, I will probably go for hourly manual mirroring v...

A new reader? Welcome to comp.os.linux.misc, read this first if you're new here (FAQ) #433
Netiquette 2007-08#1 New reader? Great! Welcome! Here's how to make best use of this newsgroup and get yourself a slice of the best help available. Now read on ... TESTING YOUR CONNECTION Errm, first a delicate point. Often users accessing a forum like this for the first time don't realize that posts don't show up immediately, certainly not for several minutes, and maybe not for some hours. In the old days, it took as long as the camel bearing the news took! So they tend to post "test" messages. Plea...

matlab reading question
Hi, I have a matlab reading question. Suppose I have a file with the following format: No. of sample in group 1 4 No. of sample in group 2 4 No. of sample in group 3 4 No. of sample in group 4 4 No. of sample in group 5 5 . . . I can read them line by line by fgets without any problem. But it's very long, I wanna read them by loop. Something like fid=fopen('inputfile','r'); for i=1:length fgets(fid); N_samples(i) = fgets(fid); end the above code doesn't work. ...

connection is expecting a read
Hi everyone i have this issue, i have a stored procedure like this: SELECT @MAX_LOAD_BEGIN = (SELECT MAX(CONVERT(INT,Load_Begin)) FROM SES WHERE PMM_DATETIME BETWEEN @dINIT_DATE AND @dEND_DATE_AUX) SELECT ISNULL(@MAX_LOAD_BEGIN,100) SELECT @DATE_MAX_LOAD_BEGIN = (SELECT MAX(PMM_DATETIME) FROM ( SELECT MAX(Load_Begin) AS 'Max_Load_Begin', PMM_DATETIME FROM ( SELECT PMM_DATETIME, MAX(CONVERT(INT,Load_Begin)) AS 'Load_Begin' FROM SES WHERE PMM_DATETIME BETWEEN @d...

Make App "Read-only"
Say I'm designing an app and I want to give admin of the app the option of making it READ-ONLY when he needs to. How will you do it ? How about this ? : Putting app tables into TABLESPACE_myapp so the admin can just do this: alter tablespace TABLESPACE_myapp read only; thanks On Mon, 24 Jul 2006 16:15:08 -0400, meathammer <googler@google.com> wrote: >Say I'm designing an app and I want to give admin of the app the >option of making it READ-ONLY when he needs to. > >How will you do it ? > >How about this ? : >Putting app tables into TABLESPACE_myapp...

[Uniface-L] Problem reading data from Oracle
I have a form A that calls form B. The first activate of form B that retrieves data from an oracle table is showing no data and the following is Written to the message frame: I/O function: S, mode: 0, on file/table: DOK index: 1 =3D ORA-01403: Keine Daten gefunden ORA-01403: Keine Daten gefunden ORACLE Driver Error [-42]: Unable to construct statement. ORACLE Driver Error [-41]: Unable to get statement from cache. ORACLE Driver Error [-38]: Select driver function failed. Then I exit form B and Form A is displayd. If a then call From B again no messages are displayed anymore and th...

read a file written in fortran
Hi, I want to read with IDL an ascii file written in Fortran 90. The file contains two types of data: double floats and "logical". For the floats no problem but for the logical (which writes either T or F (for true or false) in the ascii file) i get " READF: End of file encountered" while doing: my_float = 0.0d0 my_logical='a' readf,11,format='(1e14.6,A1)', my_float,my_logical Is there anything wrong in the format or in the initialization? Thanks in advance! On Jun 7, 4:27=A0pm, bing999 <thibaultga...@gmail.com> wrote: > Hi, ...

Sum elements of a row
Hello, I've a file like this: A ...