MATLAB Answers

Import multiple excel with different column size

조회 수: 8(최근 30일)
Nanda Dash
Nanda Dash 2021년 9월 23일
댓글: Nanda Dash 2021년 9월 29일 13:54
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.


KSSV 2021년 9월 23일
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
iwant is a cell array. You can access them using iwant{1}, iwant{3}, etc. You can convert it into matrix using cell2mat.
  댓글 수: 1
Nanda Dash
Nanda Dash 2021년 9월 29일 13:54
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);

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

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

Start Hunting!

Translated by