Import multiple excel files into multiple variables (or matrices)

77 views (last 30 days)
I'm trying to import multiple excel files inside a folder into multiple variables which means that I'm creating my variables in a loop & grabbing my "filename" of my excel sheet & declare that name as my new variable name in Matlab.
Each variable is a matrix (all numeric).
I have attached 2 example excel sheets for you to see how I setup my excel sheets.
I have looked at several codes online & this is one of them which does not work for me (I'm guessing their Matlab version is different than mine & that could possibly be the reason why but, I can't tell). I also don't understand the ".name" part in here. Also, this code does not assign the excel file name as the variable name (but I wouldn't know even if it did since the code does not work for me) but, I'm interested in seeing one example to know how to do this in the future. Please don't forget the 2 excel sheets that I provided for the format of the excel sheet. Any help is appreciated.
folder='C:\Users\JohnK\Desktop\Research\Simulation homeworks\Import_Multiple_xls';
for k=1:length(d)
filename = d(k).name ;
[X,TXT,RAW] = xlsread(filename);
% Apply the modifications
Stephen23 on 16 Oct 2018
"...& declare that name as my new variable name in Matlab."
"...& grab the filenames (permeability & porosity) to declare as variables in matlab. In my case, it is important that I do that to avoid confusion in the future."
Do NOT do this. Magically defining/accessing variable names is one way that beginners force themselves into writing slow, complex buggy code that is hard to debug. Writing complex code with magical variable names will increase the confusion in future, and should be avoided. Read this to know why:
In your case it is important to learn simpler, neater, more efficient ways to write code, like using indexing (as my answers shows you). Indexing is how experinced MATLAB users access their data, and is the correct way to "avoid confusion in the future."
Note that even if you do want to continue forcing yourself into writing pointlessly complex, slow, buggy code that magically uses filenames to define variable names, you will have to consider that this concept is very fragile: there are many filenames that are not valid variable names, for example 1.txt or a.2.txt or a-b.txt or a b.txt or millions of others. Your code would simply break with all of those. You would be much better off learning how to write code properly rather than wasting your time on this approach.
"I also don't understand the ".name" part in here."
The dir help explained that it returns a (possibly non-scalar) structure array, which has fields. One of the fields is .name. You can use it to access the names of the files/folders that dir found.

Sign in to comment.

Accepted Answer

Stephen23 on 16 Oct 2018
Edited: Stephen23 on 16 Oct 2018
Your code has a bug in it: you use an absolute file path with dir but not with xlsread. But the biggest problem is with your concept: magically defining/acessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Using filenames to define variable names is very fragile (your code will break unexpectedly).
It is much simpler and much more efficient to use indexing, like this:
D = 'C:\Users\JohnK\Desktop\Research\Simulation homeworks\Import_Multiple_xls';
S = dir(fullfile(D,'*.xls'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
[S(k).num,S(k).txt,S(k).raw] = xlsread(F);
All of the imported data and the file information (filenames, etc), is in the structure S. You can trivially refer to them simply using indexing, e.g. the second file:
and also using some of the convenient format for accessing fields of non-scalar structures:
C = {} % all of the filenames in one cell array
This means you can easily sort the filenames later, of use indexing to select the elements of S that you want to work with:
X = strncmpi('poros',C,5)
Read more here:
Yildirim Kocoglu
Yildirim Kocoglu on 17 Oct 2018
I did try what you have suggested & it worked perfectly. I greatly appreciate all the detailed help you have given us. I'm not sure if I should or should not post my code but, I hope it helps others too in their project (However, I do not guarantee it will help anyone in any way but, it can be a start rather than starting from scratch...)
Here is the final product below:
D = 'C:\Users\JohnK\Desktop\Research\Simulation homeworks\Import_Multiple_xls';
J = fullfile(D,'name*.xlsx');
S = dir(J);
FileName = {}';
%Below does not do anything special in this code (it maybe useful later for
%modifications to the code)
NAMECOMP = strncmpi('name',FileName,4);
for k = 1:numel(S)
F = fullfile(D,S(k).name);
[S(k).num,S(k).txt,S(k).raw] = xlsread(F);
E = {S.num}';
G = E{1};
Diff = zeros(size(G,1),size(G,2));
% Perform an operation on the matrices (from .xlsx files)
for i = 1 : 2 : size(FileName,1)
Diff = E{i+1} - E{i};
% Export multiple files
for j = 1:(numel(S)/2)
name = sprintf('Diff%d.xlsx',j);
fileName = fullfile(D,name);
xlswrite(fileName, Diff);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!