xls read multiple files into one script; Only reading last file

2 views (last 30 days)
I am trying to add multiple .xls files into Matlab and plot some data against time. The first file contains data for one hour of time, the second file contains data for the second hour of time and so on. I want each file to build off of the previous file. Each file contains the same column headers in the first row. When I read multiple files into the script, it only loads the last file I select.
[file_list,path_n] = uigetfile('.xlsx','Grab the files you want','Multiselect','on');
if iscell(file_list) == 0
file_list = [file_list];
end
for i = 1:length(file_list)
filename = file_list{i};
data_in = xlsread([path_n filename]);
alldata = data_in(:,:);
end
Time = alldata(:,2);
Position_Rev = alldata(:,3);
Current = alldata(:,8);
Voltage = alldata(:,9);
Altitude = alldata(:,162);
Motor_Speed = alldata(:,4);
date_time = datetime(Time,'ConvertFrom','epochtime','TicksPerSecond',1e3,'Format','dd-MMM-yyyy HH:mm:ss.SSS');
figure(1)
yyaxis left
plot(date_time,Motor_Speed)
xlabel('Time','FontWeight','bold','Fontsize',12);
ylabel('Motor Speed','FontWeight','bold','Fontsize',12);
hold on
yyaxis right
plot(date_time,Current)
ylabel('Current','FontWeight','bold','Fontsize',12);
title('Time vs. Motor Speed','FontWeight','bold','Fontsize',12);

Answers (1)

Image Analyst
Image Analyst on 10 Jan 2022
You're overwriting alldata each time. You probably want to append onto it. Instead of
alldata = data_in(:,:); % Overwrite alldata with data_in.
do
alldata = [alldata; data_in]; % Append data_in to alldata.
Be sure to initialize alldata before the loop
alldata = [];
  1 Comment
notalwayssure
notalwayssure on 10 Jan 2022
Thanks for the help! One more question, how do I get my script to bypass the first row in each excel file? My script is initially plotting a data point at each minute (0 min-59min) and then starting over from 0 min instead of continuing from 60-119min.

Sign in to comment.

Categories

Find more on Get Started with 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!