Read some data from a file containing numbers and characters
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    addou abdeljalil
 el 13 de Jul. de 2015
  
    
    
    
    
    Comentada: addou abdeljalil
 el 16 de Jul. de 2015
            Hi, my problem is like this : I want to read file dynalog (.dyn) which contain text and number.
{
    **  D Y N A M I C   B E A M    S T A T I S T I C S  **
TOTAL DOSE DELIVERED             :    228 (MU)
DOSE STANDARD DEVIATION          :   0.04 (MU)
DOSE-POSITION STANDARD DEVIATION :   0.38 (deg)
NUMBER OF SAMPLES                :   1491
                     -- STT --
   INSTANCE#     DOSE    GANTRY ANGLE    (Internal scale)
                 (MU)       (deg)
       1          0.00      0.50
       2          1.45      1.50
       3          4.36      3.60
       4          7.27      5.60
       .           .          .
       .            .          .
       176        225.73    356.40
     177        227.24    358.50
     178        228.00    359.50
     -- SEGMENT BOUNDARY SAMPLES (ACTUAL) --
   INSTANCE#     DOSE    GANTRY ANGLE    (Internal scale)
                 (MU)       (deg)
       1          0.00      0.50
       2          1.47      1.23
       3          4.37      3.28
       4          7.29      5.28
       5         10.20      7.30
   ....
  ..... }
my objective is to read data after :
 -- SEGMENT BOUNDARY SAMPLES (ACTUAL) --
 INSTANCE#     DOSE    GANTRY ANGLE    (Internal scale)
               (MU)       (deg)
Any help -:)) please
6 comentarios
Respuesta aceptada
  Azzi Abdelmalek
      
      
 el 13 de Jul. de 2015
        p='SEGMENT BOUNDARY SAMPLES'
fid=fopen('file.txt');
a=fgetl(fid);
test=isempty(regexp(a,p));
while ischar(a) & test==1
  a=fgetl(fid)
  test=isempty(regexp(a,p));
end
fgetl(fid);
fgetl(fid);
k=1;
a=fgetl(fid);
while ischar(a);
  r{k,1}=a;
  a=fgetl(fid);
  k=k+1;
end
fclose(fid)
r
2 comentarios
  addou abdeljalil
 el 13 de Jul. de 2015
				
      Editada: Azzi Abdelmalek
      
      
 el 13 de Jul. de 2015
  
			
		
  addou abdeljalil
 el 13 de Jul. de 2015
				
      Editada: addou abdeljalil
 el 13 de Jul. de 2015
  
			
		Más respuestas (2)
  Stephen23
      
      
 el 13 de Jul. de 2015
        
      Editada: Stephen23
      
      
 el 16 de Jul. de 2015
  
      Of course you can use textscan, and this is likely to be the fastest and easiest way of obtaining the file data. Here is how you can get the header data, the STT data, and the Segment Boundary Samples data:
str = fileread('temp.txt');
idx = regexpi(str,'^\s+--(\s[\w()]+)+\s--\s*$','LineAnchors');
hdr = textscan(str(1:idx(1)),'%s%f%s','HeaderLines',3,'Delimiter',{':','(',')'})
stt = textscan(str(idx(1):idx(2)),'%f%f%f','HeaderLines',4)
sbs = textscan(str(idx(2):end),'%f%f%f','HeaderLines',4)
which we can check in the command window:
hdr = 
    {4x1 cell}    [4x1 double]    {4x1 cell}
stt = 
    [7x1 double]    [7x1 double]    [7x1 double]
sbs = 
    [5x1 double]    [5x1 double]    [5x1 double]
>> hdr{1}
ans = 
    'TOTAL DOSE DELIVERED             '
    'DOSE STANDARD DEVIATION          '
    'DOSE-POSITION STANDARD DEVIATION '
    'NUMBER OF SAMPLES                '
>> stt{2}
ans =
         0
    1.4500
    4.3600
    7.2700
  225.7300
  227.2400
  228.0000
>> sbs{3}
ans =
    0.5000
    1.2300
    3.2800
    5.2800
    7.3000
Because you did not upload a sample file I used this one:
3 comentarios
  Stephen23
      
      
 el 16 de Jul. de 2015
				
      Editada: Stephen23
      
      
 el 16 de Jul. de 2015
  
			@addou abdeljalil: I believe this email is from you:
"Hi, I will come to you after your proposal for my matlab code.i use this code because it work well. In one line you wrote this form of code :
idx = regexpi(str,'^\s+--(\s[\w()]+)+\s--\s*$','LineAnchors');
But i do not understand all the symbols.
can you give me one explication please."
Lets have a look at the parts of this regular expression:
^                            - start of line
 \s+                         - space character, >= 1 times
    --                       - two hyphens
      (                      - start group
       \s                    - space character, once
         [\w()]+             - word or (), >= 1 times
                )+           - end group, >=1 times
                  \s+        - space character, >=1 times
                     --      - two hyphens
                       \s*   - any space characters
                          $  - end of the line
You will find the regular expressions documentation useful to read. You also might like to try using my FEX submssion RegexpHelper, which lets you interactively develop regular expressions:
  Sid
      
 el 13 de Jul. de 2015
        Can you use the Import tool ( MATLAB Doc, ) and import the data in as a cell array? You can also import headers separately, and the numeric data as a separate array as well.

Ver también
Categorías
				Más información sobre Text Files en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!