Re: removing string from text files.

  • Follow


Hello
I need to extract data from text file, which contain random text at a regular interval.
My text file looks something like this:

2.00 'day' 2000-01-03
'Global Mole Fraction(SOL) - Matrix' ''
    0.850421     0.662334      0.42014     0.304977      0.19464     0.107398 
   0.0579599    0.0352883    0.0235352    0.0138323   0.00822029   0.00476013 
  0.00218838  0.000692394  0.000261295  0.000107044 4.26836E-005 
4.00 'day' 2000-01-05
'Global Mole Fraction(SOL) - Matrix' ''
    0.949048     0.872466     0.744197     0.653951     0.544604     0.421489 
    0.320403     0.254397     0.207999     0.161919     0.126679    0.0969455 
   0.0640893    0.0329477    0.0187245    0.0111829   0.00651805  
and it goes on in same way for many time steps.
8.00 'day' 2000-01-09
'Global Mole Fraction(SOL) - Matrix' ''
    0.990012     0.973676      0.94251     0.914009     0.874534     0.819587 
    0.761944     0.712168     0.667202     0.619753     0.575866     0.528003 
    0.454933      0.35189     0.281888     0.229884     0.184588   


I just want to read the numbers. The code I am using now does exactly what I want, but it takes long long time(I have 1024 values at each time step and then 40 time steps). Below is the code I am using
 while k< 40     
%     for eln = 174*k+3:174*k+3+170;  % extract line number 7        
%         line = '';
%         fid = fopen('fcm_componet_1 Global Mole Fraction(SOL).txt','r+');
%         if (fid < 0)
%             printf('Error:could not open file\n')
%         else
%             n = 0;
%             while ~feof(fid),
%                 n = n + 1;                
%                 if (n ~= eln),
%                     fgetl(fid);
%                 else
% %       line = fgetl(fid)
%                     x=str2num(fgetl(fid)); 
%              [t,u]=size(x)   ;   
%                            for j=1:u
%                         h(eln-2,j)=x(j);                    
%                            end                 
%                 end                
%             end;
%                     fclose(fid);                    
%         end;
%     end
%     k=k+1
% end
Can anyone help better way(faster) of doing this?
0
Reply dhirajsingla15 (10) 6/1/2012 8:43:39 PM

dhiraj,

Check out textscan.

Gadi

On 6/1/2012 4:43 PM, dhiraj wrote:
> Hello
> I need to extract data from text file, which contain random text at a
> regular interval.
> My text file looks something like this:
>
> 2.00 'day' 2000-01-03
> 'Global Mole Fraction(SOL) - Matrix' ''
> 0.850421 0.662334 0.42014 0.304977 0.19464 0.107398 0.0579599 0.0352883
> 0.0235352 0.0138323 0.00822029 0.00476013 0.00218838 0.000692394
> 0.000261295 0.000107044 4.26836E-005 4.00 'day' 2000-01-05
> 'Global Mole Fraction(SOL) - Matrix' ''
> 0.949048 0.872466 0.744197 0.653951 0.544604 0.421489 0.320403 0.254397
> 0.207999 0.161919 0.126679 0.0969455 0.0640893 0.0329477 0.0187245
> 0.0111829 0.00651805 and it goes on in same way for many time steps.
> 8.00 'day' 2000-01-09
> 'Global Mole Fraction(SOL) - Matrix' ''
> 0.990012 0.973676 0.94251 0.914009 0.874534 0.819587 0.761944 0.712168
> 0.667202 0.619753 0.575866 0.528003 0.454933 0.35189 0.281888 0.229884
> 0.184588
>
> I just want to read the numbers. The code I am using now does exactly
> what I want, but it takes long long time(I have 1024 values at each time
> step and then 40 time steps). Below is the code I am using
> while k< 40 % for eln = 174*k+3:174*k+3+170; % extract line number 7 %
> line = '';
> % fid = fopen('fcm_componet_1 Global Mole Fraction(SOL).txt','r+');
> % if (fid < 0)
> % printf('Error:could not open file\n')
> % else
> % n = 0;
> % while ~feof(fid),
> % n = n + 1; % if (n ~= eln),
> % fgetl(fid);
> % else
> % % line = fgetl(fid)
> % x=str2num(fgetl(fid)); % [t,u]=size(x) ; % for j=1:u
> % h(eln-2,j)=x(j); % end % end % end;
> % fclose(fid); % end;
> % end
> % k=k+1
> % end
> Can anyone help better way(faster) of doing this?
0
Reply greinhorn (40) 6/4/2012 6:02:47 PM


1 Replies
33 Views

(page loaded in 0.063 seconds)


Reply: