How can I import multiple files into the MATLAB workspace?
    66 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    MathWorks Support Team
    
 el 10 de Sept. de 2012
  
    
    
    
    
    Editada: Eric Sargent
    
 el 3 de En. de 2024
            I have a set of about 50 data files which I want to import to MATLAB. Each file has several columns.
I have found that the command
load -ascii  filename.txt
works rather well for my purpose on a single file. However, I want to load about 50 different files. I would like some direction on how best to load multilple files into separate arrays.
Respuesta aceptada
  MathWorks Support Team
    
 el 17 de Mzo. de 2021
        
      Editada: MathWorks Support Team
    
 el 17 de Mzo. de 2021
  
      You can use the "load" function in a loop to load in all the files. If you place all of the files you would like to load into a single directory, you can use the "dir" function to capture the file names. Here is an example:
files = dir('*.txt');
for i=1:length(files)
    load(files(i).name, '-ascii');
end
This code will load in all of the files in the directory (assuming they are .txt files) and save them as arrays with the file name as the array name.
Other functions that might be more suitable for your application include "importdata, "textscan", "dlmread" and "readtable".
For more information on importing data into MATLAB refer to the following URL:
One more way of importing multiple files is using "Import Tool". The following documentation page provides steps required to generate the function and call the same using a MATLAB script.
1 comentario
  Stephen23
      
      
 el 17 de Feb. de 2021
				
      Editada: Eric Sargent
    
 el 3 de En. de 2024
  
			Note: This reply was in response to a previous answer and it's content has been integrated into the answer above (reply is from February 2021 and the answer above was updated in March 2021).  
-----
Note that this code is unnecessarily complex because it relies on eval to indirectly call load using command syntax. Unfortunately @MathWorks Support Team does not know how to use simpler function syntax:
A = load(files(i).name,'-ascii')
All MATLAB beginners need to learn the difference, otherwise they will write obfuscated code like this answer.
@nuvolet: your question is answered here:
Más respuestas (0)
Ver también
Categorías
				Más información sobre File Operations en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

