tcpekin
tcpekin

Reputation: 43

Importing multiple files in MATLAB

For a lab report I am writing, I need to analyze about 90 datasets. Unfortunately, they are all in text files with names such as "small single crystal si iv curve part 1 106w_m^2", with no file extension. I have them all in one folder. I would like to input each one to its own variable with it's original name for manipulation later. This requires replacing the spaces in the file name with underscores and removing the carat. I would like to make an .m file that will do this for me. So far I have this:

function [t]=makedata()
%makes data 
x=dir;
i=0;
for l=3:length(x)
    i=i+1;
    y=x(l).name;
    t=y(1:end-5);
    t=regexprep(t,' ','_');
    t=importdata(y);
end
end

Obviously this code does not work. I know my MATLAB skills are weak, so any help would be appreciated. So far, my code uses the dir command to collect data on everything in the directory, then loops through that data changing the name to something that can be output, then importing the data.

How can I make this work? I don't know how to make it output variables with varying names. I researched using the eval command but don't know how to successfully implement it, plus the FAQ said to avoid that method.

Thank you for any help. I haven't been able to find a solutoin to this online yet.

Upvotes: 2

Views: 4808

Answers (2)

Egon
Egon

Reputation: 4787

One way to get rid of those unsupported characters is the genvarname function. Personally, I'd go with creating a structure that contains all relevant information for your purpose. You can put such a structure into an array and then process every element of the array.

That way you don't really have to figure out variables names from the file names and it is far easier to process (otherwise you need a list of variable names you want to process for every next step or you duplicate your code for every seperate variable/data set, which is quite error-prone).

So I'd write a function function data = importSingleDataFile(filename) that reads the file and returns a structure like data = struct('filename', filename, 'data', yourLoadedData). Then in another function, you can loop over all files and just concatenate those structures into an array.

Upvotes: 0

Nasser
Nasser

Reputation: 13141

Why not simply obtain the list of files and process them from the ONE list? Why do you have to go through all this mess of creating one variable name for each file?

Using dir() you obtain a nice array of structs. Each struct has information of the file.

 list_of_files=dir([fullfile(pwd) '\\*.txt']);
 list_of_files(1).name
 list_of_files(2).name
 etc...

If the files have no extension, and as you say, they are all in the same directory, then simply write

  list_of_files=dir(fullfile(pwd));

Where I used pwd above for directory. Use the correct directory name in your case which can be any where on the file system.

Now you can use this ONE variable called list_of_files to open each file using its names and read the data in the file and do any other processing on the file.

Your approach of making one variable for each file name is wrong way to solve this simple problem.

Upvotes: 1

Related Questions