Effacer les filtres
Effacer les filtres

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

7 vues (au cours des 30 derniers jours)
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
  2 commentaires
Jan
Jan le 14 Mar 2019
The brute clearing header "clc, clear all, close all" does not offer any benefits here. Therefore it is a cargo cult programming (link): It migt have been useful anywhere else, so repeat it, even if it does not have any meaning here. Beside this, it wastes a lot of time by removing all loaded functions from the memory. Reloading them from the slow disk is time-consuming.
Jose Rubio
Jose Rubio le 14 Mar 2019
Thanks for the advice, Jan.

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 14 Mar 2019
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})

Plus de réponses (1)

Easwar Kumar Yarrabikki
Easwar Kumar Yarrabikki le 14 Mar 2019
%%You can try something like this, Just finding the length of vector and defining the sizes
clc
clear all
close all
fnames = dir('*.xlsx');
numfids = length(fnames);
Time=[];
Displacement=[];
for K = 1:numfids
data = xlsread(fnames(K).name);
row_length=length(data(:,1));
Time(1:row_length,K)=data(:,1);
row_length1=length(data(:,2));
Displacement(1:row_length1,K)=data(:,2);
end
  2 commentaires
Jan
Jan le 14 Mar 2019
Use size(data, 1) instead of length(data(:,1)), because the latter wastes time with creating a temporary vector only to count its elements. size can solve this directly.
Jose Rubio
Jose Rubio le 14 Mar 2019
Thanks for the quick response Easwar and Jan. This code works.
I followed Jan's suggestion and replace length(data(:,1)) with (data, 1) and it works as well.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by