MATLAB: How to import column data from multiple Excel files with different number of rows

excelimportimporting excel dataMATLAB

I have n Excels files which I need to copy and store their data into Matlab. In each Excel file, the first column represents a specific variable, e.g., "Time", and the second column is another variable, e.g., "Displacement". Each of these Excel files has the same number of columns but different number of rows. For instance, file-1 has 100 rows, but file-2 has 150 rows. Using the code below (named PlotAll), I get the following error:
Subscripted assignment dimension mismatch.
Error in PlotAll
Time (:,K) =data(:,1);
How can I solve this error? I believe it's related to having an array which changes size in the "for" loop, but I don't know how to solve it.
Thanks,
clc
clear all
close all
fnames = dir('*.xls');
numfids = length(fnames);
for K = 1:numfids
data = xlsread(fnames(K).name);
Time(:,K)=data(:,1);
Displacement(:,K)=data(:,2);
end

Best Answer

Since each file contains different number of rows, you will need to use cell array for this. Change inside the loop to
Time{K}=data(:,1);
Displacement{K}=data(:,2);
It is better to pre-allocate before the loop
Time=cell(size(fnames));
Displacement=cell(size(fnames));
When using the data
plot(Time{K},Displacement{K})