f



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 210171 articles. 11 followers. lunamoonmoon (258) is leader. Post Follow

2 Replies
750 Views

Similar Articles

[PageSpeed] 7

"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 (931)
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:

textscan wroks fine in advanced matlab, but does read anything in matlab 7
hello, i have a code i ran on Matlab 2008, and it works fine. when i tried running it on matlab 7, the command "textscan" i used, only read empty cells. does anybody know how to fix this - it's urgent... thank u. marina gandlin wrote: > hello, i have a code i ran on Matlab 2008, and it works fine. > when i tried running it on matlab 7, the command "textscan" i used, only > read empty cells. does anybody know how to fix this - it's urgent... > Well, if you were in a hurry, why did you take the time to post w/o providing any informatio...

Having issues reading excle with some empty rows
I am trying to read excel file using POI with code which looks as follows HSSFWorkbook wb = new HSSFWorkbook(stream, false); HSSFSheet sheet = wb.getSheetAt(sheetNo); int noOfRows = sheet.getPhysicalNumberOfRows(); for (int r = 0; r < noOfRows; r++) { HSSFRow row = sheet.getRow(r); if(row == null) break; //read data } What happens is if say I have an excel with 10 rows then i select the data in last 5 row and delete it and save the excel. Now if I read th...

read the data from the 4th row of a text file in matlab
I have a text file ( .txt) with two columns and around 60,000 rows. The two columns are separated by a space. The first three lines of the text have some details about the data. I want to load this file or import the data in matlab. The data basically starts only from the fourth row i.e. the first three rows are some details which i don't need. So, how do i load the data from the fourth row in matlab. The first few lines of the file are given here: #Table format: 1D # table_unit = bar # axis1_unit = s 0.00000000000000e+000 0.00000000000000e+000 1.00000000000000e-...

textscan a textfile and read the row that starts with a certain string
hello I need to read from a textfile with 400 rows code name regime start end surface altitude region regname A0212 Avelo PLUVI 01FEB 31JAN 23 344 -4 Tredor B0156 Grets NIVAL 01MAY 30AVR 51 1802 5 Fazal .... .... I just need to read one row which starts with a specific string. Say I want to read the line that starts with B0156 and that's it. i've tried: fid = fopen('info.txt'); C = textscan (fid, '%s %s %s %s %s %f %f %f %s', 'delimiter', '\t', 'headerlines', 1) fclose(fid) but I don't...

TextScan in MATLAB: Reading only numbers from a file that contain text
Hi, I have the a file that looks like that: Coordinate dump at phase x Number of particles: a x[cm] bgx y[cm] bgy 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 8.73E+00 -1.29E-04 Coordinate dump at phase x Number of particles: b x[cm] bgx y[cm] bgy 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 8.73E+00 -2.60E-04 Coordinate dump at phase x Number of particles: c x[cm] bgx y[cm] bgy 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 0.00E+00 8.73E+00 -3.93E-04 The file has more lines but follows the same pattern as described above. I want my to read only the n...

rows read vs. rows selected.
Hi All: I have a query which is running against large table. The query: SELECT DSRC_ACCT_ID, ADDR1, ADDR2, ADDR3, CITY, STATE, POSTAL_CODE, COUNT(*) FROM ERD.ADDRESSA GROUP BY DSRC_ACCT_ID, ADDR1, ADDR2, ADDR3, CITY, STATE, POSTAL_CODE HAVING COUNT(*) > 2 The applcation snapshot monitor shows: Rows selected = 181 Rows read = 646485498 Rows written = 933743357 The rows read is too high compared to rows selected. (The table has 933743360 rows) My questions is: What's the best...

Not Empty Directory appears Empty with Matlab GUI
Hello. I am using Matlab R13 and R14 and have come upon a very frustrating problem. We are using Matlab on a Linux RedHat Enterprise WS3 workstation, and working on directories shared from an XServe/XRaid system via NFS. When I cd into any of these directories in Matlab, the Current Directory tab in Matlab shows nothing although the directory is not empty and the ls command in Matlab shows the contents correctly. This is a very frustrating problem as it does not allow us to select any files via the GUI as it cannot see them. If anyone knows a solution to this problem, I would greatly appre...

Reading from sys.stdin reads the whole file in
I'm trying to read from stdin. Here I simulate a process that slowly outputs data to stdout: steve@runes:~$ cat out.py import time print "Hello..." time.sleep(10) print "World!" time.sleep(10) print "Goodbye!" and another process that reads from stdin: steve@runes:~$ cat slurp.py import sys import time for line in sys.stdin: print time.ctime(), line When I pipe one to the other, I expect each line to be printed as they arrive, but instead they all queue up and happen at once: steve@runes:~$ python out.py | python slur...

Newbie: fastcsv: Read rows, print rows
New to Ruby. It probably shows. Trying to accomplish some simple initial tasks, and study. Objectives 1. Read rows from a CSV file 2. Write rows to STDOUT A cat() equivalent. I've been looking but don't find it. Are there any good tutorials on using fastcsv? This might be a good candidate for such. Many thanks! require 'fastercsv' fastercsv.open("rfile2", "r") do |csv| csvData.each{|row| puts "row: ${row}"} end On Jun 29, 2007, at 1:50 PM, drubdrub@gmail.com wrote: > New to Ruby. It probably shows. Welcome to Ruby! > ...

Create empty dataset and populate it row by row
Hi, I'm trying to create an object of the class dataset (Stats toolbox) with a few named "columns". I want such object to initially have no rows. Then, I would like to add new rows one by one. I've tried the code below to create the dataset but I get an error. Can anyone help me with this? Thanks in advance P.S. I could not submit this question to the "answers" section of the Mathworks website % Column names are 'F1' and 'F2' myDS = dataset([],'VarNames',{'F1','F2'}) ??? Error using ==> setvarnames a...

how to find row to row correspondence between two matrices in matlab
I have two matrices A=[1 3;4 5;7 8;17 11] and B=[4 5;9 10;7 8;4 5;1 3;7 8;11 12]. I want to get a correspondence between different rows of the two matrices exactly like [I,J] = find(pdist2(A,B)==0); display([I,J]). In my case the length of A and B is huge, so performing pdist2 is not memory efficient. Can you please help me? > [I,J] = find(pdist2(A,B)==0); % This should be faster: [~, I, J] = intersect(A,B,'rows') > display([I,J]). % Bruno ...

Read from Spreadsheet File.vi reading from a specific row
hi everyone, &nbsp; I am using Read from Spreasheet File.vi to read csv files. I want to start reading the data from the 4th row everytime (there are useless headers in the first 3 rows). But the "start of read offset" lets me count characters instead of number of rows. My files have random number of characters before the 4th row so there is no way i can use this function. Is there any other way to read everything starting from the 4th row? &nbsp; &nbsp; Thanks all, &nbsp; Tai Hi BestPker, I would suggest you read all the data first and then modify the "raw"...

Re: Reading the data from the excel file into SAS by row by row
syam1919@GMAIL.COM wrote: >I am new to SAS Macro's world and currently working on SAS Macros. > >I want to read name of the file from the excel file into SAS which is >in the following format: > >Campaign_No Mail Data Socred File Quotes Files >1 Maildata_1 scored_1 Quotes_1 >2 Maildata_2 scored_2 Quotes_2 >3 Maildata_3 scored_3 Quotes_3 > >First I have to read file names contained in the first row, then second >row, then thi...

Rows/no rows
Hi, Something weird happened, the first query retruns rows but not the second Have you an explanation ? ( 9.2.0.6 AIX) Thanks in advance select * from V_PROD_INT S_PROD_INT, S_PROD_INT PR_EQUIV, S_ORG_EXT WHERE S_PROD_INT.PR_EQUIV_PROD_ID = PR_EQUIV.ROW_ID(+) AND S_PROD_INT.VENDR_OU_ID = S_ORG_EXT.ROW_ID(+) select S_PROD_INT.row_id from V_PROD_INT S_PROD_INT, S_PROD_INT PR_EQUIV, S_ORG_EXT WHERE S_PROD_INT.PR_EQUIV_PROD_ID = PR_EQUIV.ROW_ID(+) AND S_PROD_INT.VENDR_OU_ID = S_ORG_EXT.ROW_ID(+) On Aug 23, 3:30 pm, "astalavista" <...

When is empty not empty?
On PHP 5.1.6, the attached script prints: "$a isn't empty and its length is 0". Can you confirm and/or explain this? I haven't found any bug reports about it... -----BEGIN SCRIPT----- #!/usr/bin/php <? $a = `date | grep slowday`; // should come up empty if ($a !== '') echo '$a isn\'t empty and its length is '.strlen($a)."\n"; ?> ------END SCRIPT------ -- ~> cat /etc/redhat-release && uname -a Scientific Linux SL release 5.2 (Boron) Linux acerrimo.local 2.6.18-92.1.18.el5 #1 SMP Wed Nov 12 06:49:06 EST 2008 i686 athlon i38...

serial port reading the data but not displaying the read data in MATLAB
Scenario: i am currently doing a project on transmission of digital video images using matlab. what i need to do is to transmit the image data through the serial port of the computer. i have decompsed the image into individual pixels and i have written a matlab program for trnasmitting the image data. till this point i was sucessful. problem: my radio transmitter which connected to the serial port of the computer is transmitting the data the radio receiver connected to the serial of the another computer and is controlled by MATLAB. though my radio receiver is receiving the data matlab is reading the data but is not displaying the 'READ DATA' through the serial port. i am using the fscanf(obj) function for reading the data of the serial port. and can anyone tell me what is the purpose of 'terminator' is matlab. can anyone solve the problem. and please reply as soon as possible Raghu, The terminator is used to indicate the end of an ASCII string. Basically, FSCANF looks for the terminator to tell it when it has received a complete string and can attempt to process it. I suspect that you are sending binary data over the serial port. If so, it would be better to use the FREAD command. -Dave Tarkowski On Wed, 21 Apr 2004 01:41:31 +0000 (UTC), Raghu Nandan wrote: > > Scenario: i am currently doing a project on transmission of digital > video images using matlab. what i need to do is to transmit the image > data through the serial port of t...

Soft refs problem with cgi
Hi Group, I'm a relative cgi newbie. In the script below, I am trying to cycle through 3 variables using a loop to create a soft reference for each, and print the contents of each reference. When I print the dereferenced value, it is always empty - but when I print the variables themselves ($p0_mount etc), they contain the correct values. Can anybody see what I'm doing wrong. Thanks Adrian #!/usr/bin/perl -wT use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my ($ref,$p0_mount,$p1_mount,$p2_mount,$q,$val); $ENV{'PATH'}="/bin:/usr/bin"; pr...

empty
searching the manual gives lots of hits, none of which appear relevant (at least by the title). I had the following code in a scheduling class: if ($ptNum == "empty") { $ptNum = 0; $status= "e"; } I pass the string "empty" to the function as a patient number if we are creating an empty appointment slot. now, this fails because with a patient number of 0 the if returns true. I am aware of the function empty() that would work like that, but do not understand why the compare fails with the constant, "empty." I fixed the function by making it a ...

DataGridView leaves an empty row after escape pressed on new row
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg What happens is : 1. Form loads with values from database inside the DGV 2. I press the checkbox to enable editing/adding etc in the grid (see code event) Picture : http://img145.imageshack.us/img145/8819/step1yy3.jpg 3good. I click inside the NewRow and type "a" or whatever (something invalid) ... Picture : http://img80.imageshack.u...

read in Matlab
I have 100 files called arbitrarily. All files have numeric data. I want to store in vector all of the 100's files names and than run a m-file and calling up the name from vector and open thoes files and do some analisys on this numeric data. I can't just write this loop to help me with all this files. Please help, I run agains time. Thank You Hi Artur, sorry, but I don't understand your problem! Regards, Stefan It's probably so simple to hard to understand. Ok one more. You have 100 file with some data. You want to open each and do the fft analisys. It will take some time,...

Re: Reading the data from the excel file into SAS by row by row #3
In addition to the good points raised by Toby and David ... I see no connection between the code shown and the stated subject (Excel, rows, columns). On Tue, 6 Dec 2005 08:28:41 -0800, syam <syam1919@GMAIL.COM> wrote: >Hi All, > >I have written this code but I am facing little problems with this can >anybody suggest upon this; > > >%LET CSYAM_LIBLOC = 'C:\data\syam\'; >%LET CDRIVE_LIBLOC = 'C:\data\mail id data\'; > >LIBNAME CSYAM &CSYAM_LIBLOC; >LIBNAME CDRIVE &CDRIVE_LIBLOC; > >%MACRO NUM_OBSER(START= ,END= ); > > ...

Reading the data from the excel file into SAS by row by row and cell by cell
I am new to SAS Macro's world and currently working on SAS Macros. I want to read name of the file from the excel file into SAS which is in the following format: Campaign_No Mail Data Socred File Quotes Files 1 Maildata_1 scored_1 Quotes_1 2 Maildata_2 scored_2 Quotes_2 3 Maildata_3 scored_3 Quotes_3 First I have to read file names contained in the first row, then second row, then third row......... For example, in campaign_no:1, I have to get the file name Maildata_1, then Scored_1, Quotes_1 one by one.......... A...

Re: Reading the data from the excel file into SAS by row by row #2
syam, You never create a macro var IND hence sas can't find one, hence the message you are recieving. You do however create a datastep variable Ind (via the do loop index var). If that is what you are wanting to assign to the variable INDI then best fix I think would be to change your do loop index var to INDI. Toby Dunn From: syam <syam1919@GMAIL.COM> Reply-To: syam <syam1919@GMAIL.COM> To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Reading the data from the excel file into SAS by row by row and cell by cell Date: Tue, 6 Dec 2005 08:28:41 -0800 I have written thi...

Delete row of cell array where any one cell in the row is empty or NaN
Hi I know I should know this, but I just can't get the right syntax :-( Given a={'one', [],'three';[],'five','six';'seven','eight',[];'ten','eleven','twelve';[],[],'fifteen'} a = 'one' [] 'three' [] 'five' 'six' 'seven' 'eight' [] 'ten' 'eleven' 'twelve' [] [] 'fifteen' What is the syntax to delete any row that contains at least one empty cell (or a NaN - though there are no NaNs in my (a)). So given (a) above I should only be left with 'ten' 'eleven' 'twelve' I've played around with a(cellfun(@isempty,a)) = [] but just can't get it right Thanks in advance "Laurence Johns" <matt1234au@yahoo.com> wrote in message <h321ka$3vb$1@fred.mathworks.com>... > Hi > > I know I should know this, but I just can't get the right syntax :-( > Given > > a={'one', [],'three';[],'five','six';'seven','eight',[];'ten','eleven','twelve';[],[],'fifteen'} > > a = > > 'one' [] 'three' > [] 'five' 'six' > 'seven' 'eight' [] > 'ten' ...

Web resources about - textscan - how to read empty rows? - comp.soft-sys.matlab

TextScan on the App Store on iTunes
Get TextScan on the App Store. See screenshots and ratings, and read customer reviews.

ONLINE FROM TODAY: ANCIENT TEXTS FROM THE VATICAN APOSTOLIC AND BODLEIAN LIBRARIES
VaticanCity, 3 December 2013 (VIS) – The Vatican Apostolic Library (BAV)and the Bodleian Libraries of the University of Oxford have joinedforces ...

Selections from Hilaire Belloc's Verse and Prose
Selections from Hilaire Belloc's Verse and Prose

MATLAB Documentation - MathWorks Deutschland
MATLAB is a high-level language and interactive environment for numerical computation, visualization, and programming.

Pigs, Gourds, and Wikis
As the #myFirstMac stories start to spread on Twitter, I think back to my first Mac. It wasn't mine at all. It belonged to a boyfriend in college. ...

Pure One Classic Series II DAB adds Listen Later function
With PVRs becoming the norm people have gotten used to watching what they want, when they want and Pure thinks that the radio should be no different ...

Pure Digital Tempus-1S Comes With Natural Sounds
Digital-Lifestyles We reported the effects of Technology on the Media and Communications between 2001 and 2009.

Romhacking.net - Documents - CNROM CHR to MMC3 CHR
Documents, Utilities, ROM Hacks, Translations, Wiki, Community, and more! The central hub of the ROM hacking community!

Gathering and Greeting: Notes to the Resources
Gathering and Greeting: Notes to the Resources

MATLAB Examples - MATLAB & Simulink - MathWorks Deutschland
MATLAB Examples - The Language of Technical Computing.

Resources last updated: 2/25/2016 5:48:04 AM