f



Text file with numbers and text, want to just read numbers

I have a series of text files I want to read.  The files 
begin with header information.  The number of rows of 
header information changes, there is no commenting symbol 
such as %%, and the header information sometimes has 
numbers such as a date or a time.  I want to read in the 
number and only the numbers.  

This is an example of a file: (Again, the header info 
changes, different # of lines, different text)

Vector Signal [Measurement4]:  09/27/2006, 09:05:24
Units: EU
Time	 Real
            0	      0.053732
     9.6e-005	       0.11968
     0.000192	      0.085483
     0.000288	       0.11968
     0.000384	       0.18562

Currently I have Matlab code something like this:

[filename, pathname, filterindex] = uigetfile
({'*.txt','Text file (*.txt)';'*.*','All Files 
(*.*)'},'Pick Time History File');
fid = fopen(filename);
C_text = textscan(fid,'%s');
fclose(fid);


I tried to sort out the header from C_text by creating a 
loop searching for numbers.  Example, seeing if the 
absolute value is greater than one.  This method failed 
when it ran into dates and times.  

Is there a better way?  I've been trying to use textscan, 
but I have also been looking at fscan and textread.  I 
think there may be some formatting with C_text that could 
work, but I couldn't get it to work.  





0
7/3/2008 8:18:01 PM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
1032 Views

Similar Articles

[PageSpeed] 51

"Matt " <matthew.r.duncan@boeing.com> wrote in message
<g4jc5p$341$1@fred.mathworks.com>...
> I have a series of text files I want to read.  The files 
> begin with header information.  The number of rows of 
> header information changes, there is no commenting symbol 
> such as %%, and the header information sometimes has 
> numbers such as a date or a time.  I want to read in the 
> number and only the numbers.  
> 
> This is an example of a file: (Again, the header info 
> changes, different # of lines, different text)
> 
> Vector Signal [Measurement4]:  09/27/2006, 09:05:24
> Units: EU
> Time	 Real
>             0	      0.053732
>      9.6e-005	       0.11968
>      0.000192	      0.085483
>      0.000288	       0.11968
>      0.000384	       0.18562
> 
> Currently I have Matlab code something like this:
> 
> [filename, pathname, filterindex] = uigetfile
> ({'*.txt','Text file (*.txt)';'*.*','All Files 
> (*.*)'},'Pick Time History File');
> fid = fopen(filename);
> C_text = textscan(fid,'%s');
> fclose(fid);
> 
> 
> I tried to sort out the header from C_text by creating a 
> loop searching for numbers.  Example, seeing if the 
> absolute value is greater than one.  This method failed 
> when it ran into dates and times.  
> 
> Is there a better way?  I've been trying to use textscan, 
> but I have also been looking at fscan and textread.  I 
> think there may be some formatting with C_text that could 
> work, but I couldn't get it to work.  
> 
> 
> 
> 
> 

I would read the headers with fgetl and then decide if the
first character is a letter, which indicates a header line
in your example.  Once past the headers, I would then read
the data with a textscan.

0
par (250)
7/4/2008 6:55:06 AM
"Matt " <matthew.r.duncan@boeing.com> wrote in message 
<g4jc5p$341$1@fred.mathworks.com>...
> I have a series of text files I want to read.  The files 
> begin with header information.  The number of rows of 
> header information changes, there is no commenting symbol 
> such as %%, and the header information sometimes has 
> numbers such as a date or a time.  I want to read in the 
> number and only the numbers.  
> 
> This is an example of a file: (Again, the header info 
> changes, different # of lines, different text)
> 
> Vector Signal [Measurement4]:  09/27/2006, 09:05:24
> Units: EU
> Time	 Real
>             0	      0.053732
>      9.6e-005	       0.11968
>      0.000192	      0.085483
>      0.000288	       0.11968
>      0.000384	       0.18562
> 
> [..]

Hi,
you may try txt2mat from the file exchange which heavily 
uses regular expressions to separate the header from the 
numeric data (if the user does not supply the number of 
header lines). It returns this data as a numeric matrix 
variable "A" and the header lines as a string "hl" if you 
type

[A,ffn,nh,SR,hl] = txt2mat('c:\myFile.txt');

You can then parse "hl" separately to extract additional 
numbers.

Regards
Andres
0
rantore (21)
7/4/2008 7:24:02 AM
"Matt " <matthew.r.duncan@boeing.com> wrote in message 
<g4jc5p$341$1@fred.mathworks.com>...
> I have a series of text files I want to read.  The files 
> begin with header information.  The number of rows of 
> header information changes, there is no commenting symbol 
> such as %%, and the header information sometimes has 
> numbers such as a date or a time.  I want to read in the 
> number and only the numbers.  
> 
> This is an example of a file: (Again, the header info 
> changes, different # of lines, different text)
> 
> Vector Signal [Measurement4]:  09/27/2006, 09:05:24
> Units: EU
> Time	 Real
>             0	      0.053732
>      9.6e-005	       0.11968
>      0.000192	      0.085483
>      0.000288	       0.11968
>      0.000384	       0.18562
> 
> Currently I have Matlab code something like this:
> 
> [filename, pathname, filterindex] = uigetfile
> ({'*.txt','Text file (*.txt)';'*.*','All Files 
> (*.*)'},'Pick Time History File');
> fid = fopen(filename);
> C_text = textscan(fid,'%s');
> fclose(fid);
> 
> 
> I tried to sort out the header from C_text by creating a 
> loop searching for numbers.  Example, seeing if the 
> absolute value is greater than one.  This method failed 
> when it ran into dates and times.  
> 
> Is there a better way?  I've been trying to use textscan, 
> but I have also been looking at fscan and textread.  I 
> think there may be some formatting with C_text that could 
> work, but I couldn't get it to work.  
> 
> 
> 
> 
> 

1) read all lines using
   s = textscan(filename,'%s','delimiter','\n') ;

2) determine at which lines data starts, e.g. line X
   This is the real problem!
   p = regexp(s,'[A-Za-z]') will give a cell array with non-
empty cells when a line contains text (but how to deal with 
scientific notation ...). See help regexp for more options.
   X = max(find(~cellfun('isempty',p))) + 1

3) read again skipping the first (X-1) lines:
   data = textread(filename,'%f','headerlines',X-1) ;

4) 
0
DELjos (627)
7/4/2008 9:32:02 AM
"Jos " <DELjos@jasenDEL.nl> wrote in message 
<g4kqmi$ook$1@fred.mathworks.com>...
[..]
> 2) determine at which lines data starts, e.g. line X
>    This is the real problem!
>    p = regexp(s,'[A-Za-z]') will give a cell array with 
non-
> empty cells when a line contains text (but how to deal 
with 
> scientific notation ...). See help regexp for more 
options.
>    X = max(find(~cellfun('isempty',p))) + 1
> 
[..]

That's the crux! Depending on what you know in advance 
about the file, you have to care about scientific notation, 
as you mentioned, final header lines containing delimiters, 
signs, whitespaces or other special characters only or just 
being empty, and possibly NaN and Inf occurrences in the 
number section.
All in all, this can result in quite a lengthy piece of 
code.

Andres
0
rantore (21)
7/4/2008 10:52:01 AM
Reply:

Similar Artilces:

reading in file with text and numbers to be able to access numbers
Hi. I am trying to read in a file that was created by another application. The first two lines of this file are composed of text that I don't need. Following are 100 lines of numbers that I do need. Any idea on how to load this file without making an overly specific script? The number of rows of numbers the other application generates varies. Thank you for any tips! Look at the TEXTREAD function. One of the options is 'headerlines', to tell it the number of lines to skip at the beginning of the file. So you would do something like this: n = textread('myfile.txt','...

to read wanted only numbers in text file
Hi, I have the following text. I want to choose numbers except strings in text file. How can I do it? # t m gamma 3.6442E03 2.2342E01 1.2321E00 3.2232E03 2.2342E01 1.2321E00 Hi, M=textread('text.txt','', ... 'commentstyle','shell', ... 'delimiter',' ') J�r�me ...

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

wants to read number of text file from one folder...
Hi ALL, I want to read in numbers of .txt files (47 txt files) from one folder. eac= h and every txt file has different no. of variables and observatios. I want= to read every text file from this folder at same time and wants to put dat= a in one final dataset. is it possible?=20 or we have to create seperate dataset and then merge all dataset? please guide, Thanks. ...

Reading a text file into a Matlab Matrix
Hello there, I have a text file that contains the following; word11 word12 word13 word14 word21 word22 word23 word24 word31 word32 word33 word34 I want to read it into a 3x4 matrix (each word in a separate cell) I tried to use; data = textread('fileName.txt', '%s', 'delimiter', '\n') - puts each line in one cell data = textread('fileName.txt', '%s', 'delimiter', ' ') - puts each word in one cell but in a 12x1 matrix/array [col1 col2 col3 col4] = textread('tE.txt', '%s %s %s %s', 'delimit...

Reading Text File, Text Scrolling and erase line from a text file
Hi , How can I Reading Text File, then Text Scrolling by means of keys "page UP" "page Down" and erase a specific line from a text file , when press "F2" key? Best Regards, Lidia from Poland -- ========================================= Pozdrawiam Lidiaa Lidiaa schrieb: > Hi , > > How can I Reading Text File, then Text Scrolling by means of > keys "page UP" "page Down" and erase a specific line from a text > file , when press "F2" key? > > ...

Reading numbers from a text file
Hi, i have a text file that has the following data: 19.99 12 Java T-shirt 9.99 8 Java Mug 15.99 13 Duke Juggling Dolls 3.99 29 Java Pin 4.99 50 Java Key Chain I want to read in the data from the text file and calculate prices, but i get the following error: java.lang.NumberFormatException: For input string: "9.99 8 Java Mug" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1213) at java.lang.Double.parseDouble(Double.java:202) at FileIO...

Read text and number from a file
I have a text file as follows: str.str1.str2 0 105 72 69 73 str.str3.str4 0 108 73 70 str.str4.str5 0 109 89 12 98 109 Basically each line starts from a string followed by some integers. The delimiter is '\t' The strings in each line are not of the same length. And there are different numbers of numeric numbers in each line. So how to read it out correctly? say, read a line each time and assign the string to a string, numbers to a number variable (vector). Thanks. ...

How to read numbers in a text file efficiently
How to read the numbers in a text file in the following form ISSUE K1 K2 K3 "130108074" "4" "3" "2" "130108073" "6" "1" "1" "130108072" "2" "5" "1" "130108071" "2" "6" "5" "130108070" "1" "2" "2" "130108069" "5" "6" "1" "130108068" "3" "2" "1" "130108067" "5" "6" "5" ...

Scilab
I have a text file that has both string and numbers in it. It looks something like this: density 12345 viscosity 678910 surface tension 11121314 ......... and so on. the file is called "properties.txt" and it is a list of fluid properties that I need to use for my calculations. What command could I use to extract the numbers from the text file and assign them to variables in scilab. I have tried: mopen file('open',...) fscanfMat read nothing seems to get the job done. fscanfMat came close but it reads only the first number and not the rest of them....

reading in Text files with words and numbers
I have a text file with some numbers and words, I want to load the numbers into matlab variables but somehow I don't know hot tell matlab to disregard all the words or spaces. My files look like so %%%FILE 1 %%%%%%%% 1000.0 400.0 2.5-4 4.-5 (denw,deng,viscow,viscog) 0.080 0.00 (residual saturations non-wetting) 3.52 3.0 (entry pressure Pd and Lambda) 0.00 0.00 9.810 (gravity) 0.40 0.00 (porosity and compressibility) %%%%END OF FILE %%%%%% %%%%FILE 2 %%%%%%%%% 50 50 1 1 (total number of nodes = nx * ny * nz) 2 (water nbcpr) 0 1 50 1 1 1 1 0.0d0 0.d0 1000.d0 2 50 50 1 1 1 1 0.0d0 0.d0 100...

Read real numbers from text file
Hi everybody, I'm new with Fortran programming and I'm trying to read number from an unformatted text file. The content of the file is like this: &FEEDBK FBKBEG = 47.1 , FBKCON = 0.1 , FBKCTIM = 2 , .... and I've used this piece of code to read it: program ReadJettoIn ! Test program to read profile controller parameters from the ! ttala's jetto.in file ! ! 5/03/2004 G. De Tommasi ! ! Variables declarations character*80 fname , item character*11 pName(25) character dummy integer fileid,...

Reading a number of files and and searching for text
Hi Guys, I have a number of results files inside a /results folder such as /results/results1.txt /results/results2.txt ... ... and so on. Inside each of these files are messages either This feature Passed This feature Failed What I want to do is read all of these files /results/results* and pick out the messages that say FAILED and store them in a variable or whatever so I can print them to screen. Any you guys any bright "easy" ideas? Help much appreciated.Thanks. -- Posted via http://www.ruby-forum.com/. On 25 Apr 2008, at 10:28, Paul Donaghy wrote: > Hi Guys, > >...

read certain number in a text file
Hi all: I have hundreds of text file like the following one: ============== DATA FILE description = {Created [01-26-2010 10:12:42] } samples = 128 lines = 14 bands = 64 data type = 2 interleave = BIL wavelengths = { 535.2, 539.9, 544.5, 549.2, 553.9, 558.6, 563.3, 568.0, 572.7, 577.4, 582.1, 586.8, 591.5, 596.2, 600.9, 605.6, 610.3, 614.9, 619.6, 624.3, 629.0, 633.7, 638.4, 643.1, 647.8, 652.5, 657.2, 661.9, 666.6, 671.3, 676.0, 680.7, 685.3, 690.0, 694.7, 699.4, 704.1, 708.8, 713.5, 718.2, 722.9, 727.6, 732.3, 737.0, 741.7, 746.4, 751.1, 755.7, 760.4, 765.1, 769.8, 774.5, 779.2, 7...

Web resources about - Text file with numbers and text, want to just read numbers - comp.soft-sys.matlab

Number - Wikipedia, the free encyclopedia
A number is a mathematical object used to count , label, and measure . In mathematics , the definition of number has been extended over the years ...

Number - Wikipedia, the free encyclopedia
A number is a mathematical object used to count , label, and measure . In mathematics , the definition of number has been extended over the years ...

Closing the Gap: Five numbers that should shame Australia
Indigenous Australians continue to die younger, be out of work more and have less education than other Australians, the annual Closing the Gap ...

Double take: black Maserati with Perth City Council number plates turns heads
A black Maserati featuring the number plates and crest of the Perth City Council does have a direct connection to the City of Perth, WAtoday ...

Double take: black Maserati with Perth City Council number plates turns heads
... driver did a double take on Tuesday when he saw a black four-door Maserati saloon pull into a Perth carpark - with Perth City Council number ...

NDIS ACT rollout improves but numbers underestimated: quarterly report
The rollout of the National Disability Insurance Scheme in the ACT is on track with complaints slowing down, but the number of Canberrans deemed ...

Closing the Gap: Five numbers that should shame Australia
Indigenous Australians continue to die younger, be out of work more and have less education than other Australians, the annual Closing the Gap ...

Sparkle Updater vulnerability puts ‘huge’ number of Mac apps at risk of hijacking
A new vulnerability in Sparkle has put a “huge” number of Mac applications at risk for hijacking. For those unfamiliar, Sparkle is a tool used ...

Apple is looking to expand the number of clock faces in future versions of WatchOS
A new job listing, first spotted by AppleInsider , suggests that future versions of the Apple Watch and WatchOS should offer a number of new ...

“Huge” number of Mac apps vulnerable to hijacking, and a fix is elusive
Enlarge (credit: vulnsec.com) Camtasia, uTorrent, and a large number of other Mac apps are susceptible to man-in-the-middle attacks that install ...

Resources last updated: 2/13/2016 1:24:04 PM