automate the transfer of text files iteraton wise
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    RAJ
 el 22 de Ag. de 2019
  
    
    
    
    
    Comentada: Steven Lord
    
      
 el 30 de Ag. de 2019
            Dear All
I have 730 text files named as file_1.txt, file_2.txt, ...,file_730.txt which need to be transfered in MATLAB such that file_1.txt will be transfered during 1st iteration and so on. I don't know how to address such automation. Please help ! 
Below is the code for the 1st file to be read during 1st iteration:
fID = fopen('file_1.txt','r');
diffusion=textscan(fID,'%n %n %n','headerlines',7,'Delimiter','');
fclose(fID);
0 comentarios
Respuesta aceptada
  Stephan
      
      
 el 22 de Ag. de 2019
        
      Editada: Stephan
      
      
 el 22 de Ag. de 2019
  
      name = "file_1.txt";
filelist = string(zeros(730,1));
diffusion = cell(730,1);
for k = 1:730
    number = string(k); 
    filelist(k)=replaceBetween(name,"_",".",number);
    fID = fopen(filelist(k),'r');
    diffusion{k} = textscan(fID,'%n %n %n','headerlines',7,'Delimiter','');
    fclose(fID);
end
2 comentarios
  Steven Lord
    
      
 el 30 de Ag. de 2019
				You actually don't need replaceBetween here. The + operator for string arrays knows how to convert numbers into a string and concatenate that new string with the original string.
diffusion = cell(730, 1);
for whichfile = 1:730
    thename = "file_" + whichfile + ".txt";
    fID = fopen(thename, 'rt');
    diffusion{k} = textscan(fID,'%n %n %n','headerlines',7,'Delimiter','');
    fclose(fID);        
end
In fact, this is vectorized which can be useful if you need the list of all the file names.
whichfile = (1:10).';
thename = "file_" + whichfile + ".txt"
Implicit expansion works too.
ind = 1:5;
A = "x(" + ind.' + "," + ind + ")"
Más respuestas (0)
Ver también
Categorías
				Más información sobre File Operations 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!


