MATLAB Answers


How to import column "x" from multiple excel workbooks and create a new excel file with imported columns?

Asked by Kundan
on 18 Jul 2013

I am extremely new to MATLAB and with no prior programming experience. I have a fairly large dataset, stored in excel(*.xlsx) format(2 sheets in each workbook, each sheet size with approx 660000 x 30 cells).

I need to generate spreadsheet for individual variables which are stored in same column in multiple excel workbooks (e.g. variable lat would be in column2 in all workbooks. The script that I am using is working only for 10 spreadsheets and after 10 spreadsheet I am getting 0 values.

|%This script is used in extracting single excel column from multiple files 
%and writing them in another excel file
%% Load Data
dirs=dir('D:\NEE\*.xlsx');%read all the *.xlsx in the directory
%% Manipulate filenames
dircell=struct2cell(dirs); %Change the filename to strcture
%% Read data up-to Row 17519 
num = (numel(filenames)); %
Newfile = zeros(17519,num); % Allocating memory 
%% Iteration
for i = 1:num
    Newfile(:,i) = xlsread(filenames{i}, 1, 'L2:L17520'); % create array
%% create a new excel file
xlswrite('LAT07.xls',Newfile); %|

Please tell me what I am doing wrong. Your help is greatly appreciated.



Looks good to me. As an experiment, if you change the call to xlsread to read from filenames{1} (first file) rather than filesnames{i}, does that result in same column of data 120 times, or does that also turn to all zero columns after the tenth?

What happens if instead of running the loop from 1:N you start at 11:N?

Log in to comment.

0 Answers

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!