Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Getting float values from a formatted text file
Replies: 2   Last Post: Mar 27, 2013 4:46 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
dpb

Posts: 8,182
Registered: 6/7/07
Re: Getting float values from a formatted text file
Posted: Mar 27, 2013 4:46 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 3/27/2013 1:26 PM, dpb wrote:
> On 3/27/2013 12:41 PM, Abubakar Rehman wrote:
> ...
>

>> My text file looks like the following
>>
>> 32 Fed 2044 25:61:61
>> Asdfg-Asdfg
>>
>>
>> Data Unavailable
>>
>>
>> Yaqw (UUUUU) Qwe (321) (deg) Asdfg (321) (deg) Yxcv (321) (deg)
>> ----------------------- --------------- -----------------
>> ----------------
>> 32 Def 2022 10:00:00.040 56.001 -0.001 0.137
>> 32 Def 2022 10:00:00.060 56.000 -0.001 0.112
>> 32 Def 2022 10:00:00.080 56.000 -0.001 0.112
>>
>> Yaqw (UTCG) i (re) h (re) g (re) ------....--------- --------
>> 32 Def 2022 10:00:00.000 6378.139002 -0.000844 0.000000
>> 32 Def 2022 10:00:00.020 6378.139001 -0.000504 0.000000
>> 32 Def 2022 10:00:00.040 6378.139001 -0.000255 0.000000
>>
>>
>> I want the values 56.001, -0.001, 0.137, 6378.139002 etc.. in an array..
>>
>> I have written the follownig code but it doesn't work. It returns error
>> 'First argument must be a string'

>
> Oh, for Fortan-like fixed-width format fields...(are you _still_
> listening TMW? :) )
>
> Don't see any code so can't comment on the error but textscan() should
> solve your problem (albeit w/ returned values in a cell array rather
> than simple 2D array of doubles).
>
> fmt=[repmat('%*s ',1,4) repmat('%f ',1,3)];
> fid=fopen('yourfile','rt');
> c=textscan(fid,fmt,'headerlines',9,'collectoutput',1)
> c =
> [3x3 double]
> c=[c;textscan(fid,fmt,'headerlines',2,'collectoutput',1)]
> c =
> [3x3 double]
> [3x3 double]
>
> fid=fclose(fid);
>
> c{:}
> ans =
> 56.0010 -0.0010 0.1370
> 56.0000 -0.0010 0.1120
> 56.0000 -0.0010 0.1120
> ans =
> 1.0e+03 *
> 6.3781 -0.0000 0
> 6.3781 -0.0000 0
> 6.3781 -0.0000 0
>
> Repeat as needed; you can build a loop if size of file is unknown that
> quits on feof(fid) as long as you know the spacing between groups of data.
>
> Or, if that spacing isn't known, you can always revert to fgetl() and
> read a line at a time until you find the next Yaqw string in the header
> line.
>
> Too bad textread() isn't as flexible to return the array directly
> instead of the cell, Steven. :)


But, in this case cell2mat() is sufficient so isn't too bad...

>> cell2mat(c)
ans =
1.0e+03 *
0.0560 -0.0000 0.0001
0.0560 -0.0000 0.0001
0.0560 -0.0000 0.0001
6.3781 -0.0000 0
6.3781 -0.0000 0
6.3781 -0.0000 0
>>

--




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.