Import multiple excel with different column size

7 views (last 30 days)
Hi,
I have 100s of excel files to import. In some excel I have 40 columns and in some excel I have 60 columns.
For each type of excels 40 cloumns are common and having same header names. But the 20 columns in some file comes in middle of 40 columns. Is there any easy way to import all these excels together?
Thanks in adavance for your support.

Answers (1)

KSSV
KSSV on 23 Sep 2021
xlFiles = dir('*.xlsx') ; % you are in the folder of files
N = length(xlFiles) ; % Total number of files
iwant = cell(N,1) ;
for i = 1:N % loop for each file
T = readtable(xlFiles(i).name) ; % read data as table
iwant = table2array(T) ; % save data as array
end
iwant is a cell array. You can access them using iwant{1}, iwant{3}, etc. You can convert it into matrix using cell2mat.
  1 Comment
Nanda Dash
Nanda Dash on 29 Sep 2021
Hi, Thanks for the quick answer. This is how I am importing now. This sheet is having 40 variables
but in some sheets I have 60 variables. those extra 20 variables comes before "AbsoluteTime" column.
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 40);
% Specify sheet and range
opts.Sheet = "Cycle_1";
opts.DataRange = "A2:";
% Specify column names and types
opts.VariableNames = ["Temperature1", "Temperature2", "Temperature3", "Temperature4", "AbsoluteTime", "RelativeTime", "Minutes"];
% Import the data
iwant = readtable("C:\Data\Excel\Cycle_0.xlsx", opts, "UseExcel", false);

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!