Import file excel with different length

Hi I would like to know if there is an easy way to import Excel files of different length. In particular, each Excel file is composed by 4 variables.
I should create an array that contains each vector of the files and put them by column.
In detail, I enclose my code.
clc
folder='C:\Users\....';
d=dir(folder)
e={d.name}
f=e(~cellfun(@isempty,regexp(e,'.+(?=\.xlsx)','match')))
for k=1:numel(f)
data{k,1}=xlsread(f{k})
end
This is the matrix that contains all the data that then groups them together for similar variables:
M=cell2mat(data');
The problem is that each vector of array has a different length. (give error)
The goal is then separted each variable in this way:
cc11=M(:,1:4:end);
tt11=M(:,2:4:end);
cc22=M(:,3:4:end);
tt22=M(:,4:4:end);
Thanks a lot in advance and I hope someone can give me suggestions!!!!

10 Comments

Isn't
folder='C:\Users\....';
d=dir(fullfile(folder,'*.xlsx');
for k=1:numel(d)
data{k,1}=xlsread(fullfile(folder,d(i).name));
end
the same thing as your regular expression and search?
As far as the actual posed Q?, what's the end result of what to do with the resulting data (beyond the expressed wish to put inconsistent data sizes into an array that doesn't work well)? Just treating them as cell array may be the best route; otherwise you'll have to either truncate the longer or fill the shorter with NaN or somesuch. What's to be done with the various sets together rather than individually and how to consider where there aren't data (or what to do with the excess)?
cc11=M(:,1:4:end);
tt11=M(:,2:4:end);
cc22=M(:,3:4:end);
tt22=M(:,4:4:end);
These are my variables that I should have organized in this way. for example, ccq is an array that contains the same variable for each file ... (but each column has a different length).
dpb
dpb on 7 May 2020
Edited: dpb on 8 May 2020
Yes, you already said that -- but what you haven't addressed is what you think should happen for the ones that don't have as many elements (if going to keep the longest)? Or, just throw away everything except the shortest?
What are you going to do next presuming you were to somehow put them all in one array of variable lengths?
I thought you didn't understand the question.
Imagine that each file contains x1, x2, y1, y2.
I want to be able to consider these curves together for all the files.
dpb
dpb on 8 May 2020
Edited: dpb on 8 May 2020
"I want to be able to consider these curves together for all the files."
And what does that mean, precisely? IOW, what operation(s) does "consider" consist of?
One has to have a problem specification nailed down before trying to write code.
I have these different files, for example I wanted to separate a single variable and create a single array:
cc1 = [x1 (file1) x1 (file2) .... x1 (file3)];
tt11 = [y1 (file1) y1 (file2) .... y1 (file3)];
.....
The problem is that x1 (file1) and x1 (file two) have two different lengths..
dpb
dpb on 11 May 2020
Edited: dpb on 11 May 2020
We're going 'round and 'round, plowing the same field over and over...
What you want is clear; what you really need isn't necessarily what you think you need.
What you are going to do with this variable when you get it will define the "how" of what need to do...or, that might also mean there really isn't any need to create a single variable at all because you can do whatever it is that needs to be done on each piece separately.
The problem is you haven't told us what that is -- you have fixated on the idea of having to have a single array to the exclusion of telling us what the problem/end objective is in sufficient detail to be able to advise the most effective approach.
But .... I want to plot curves that have different vector lengths
If you have any suggestions, I am very happy, however thank you very much.
dpb
dpb on 16 May 2020
Edited: dpb on 17 May 2020
AHA! :) Now we have an objective to deal with!!! As I suspected could be the case, this doesn't require having all the data at once, and since you would have to loop to get the various files to put them together anyway, just do what needs doing instead...don't bother to do what you don't have to do.
So, in summary:
You can plot multiple curves of any length on the same axis one at a time without the data all being in a single array...just loop over the files and plot the data wanted.
After the first, set hold on to add subsequent to the same axes.
There are any number of examples in the documentation and on the forum...the most basic but adequate for you to accomplish the above stated objective is <Add Line Plot to Existing Axes>
But, if you're adamant that you must have the array first, then the solution to augment the shorter with NaN will work ok; plot and friends ignore NaN in the data arrays. But, again, that's still more effort to go to than needed for the purpose as well as creating unnecessary copies of the same data that takes up memory...
Fredic
Fredic on 18 May 2020
Edited: Fredic on 18 May 2020
ok thanks, I will try it

Sign in to comment.

Answers (1)

You could specify a range that has the length of the longest vector (xlrange), which will load in empty cells as NaN, which you could then later remove. Or load in the 4 vectors seperately from the start (also can be done by specifying the range)

7 Comments

Yes but the problem that I have to create then this part:
cc11=M(:,1:4:end);
tt11=M(:,2:4:end);
cc22=M(:,3:4:end);
tt22=M(:,4:4:end);
Could you give an example of your data (what does 'data' look like after importing)? Because those lines should work for getting every 4th column, if that is what you are trying to do. After splitting them up, you can then remove the NaNs
data =
4×1 cell array
{1794×4 double}
{ 538×4 double}
{ 386×4 double}
{ 751×4 double}
Error using cat
Dimensions of arrays being
concatenated are not
consistent.
Error in cell2mat (line 75)
m{n} =
cat(2,c{n,:});
Error in Fitting_function
(line 13)
M=cell2mat(data');
appears this error...
Ah ok this helps!
Maybe make a loop that does cell2mat(data(i,1)) for the value 1:4, and then add them to the cc11 variables within the loop. e.g. something like:
cc11 = []; %
for 1:size(data,1)
M = cell2mat(data{i,1})
cc11 = [cc11; M(1,:)]
end
(!Did not test syntax)
not work!
Index in position 1 is
invalid. Array indices must
be positive integers or
logical values.
Error in Fitting_function
(line 17)
M = cell2mat(data{i,1 });
the code stop here.
for i = 1:size(data,1), sorry forgot the i = part
eh no.. not work..
Brace indexing is not
supported for variables of
this type.
Error in cell2mat (line 42)
cellclass = class(c{1});
Error in Fitting_function
(line 19)
M = cell2mat(data{i,1});

Sign in to comment.

Asked:

on 7 May 2020

Edited:

on 18 May 2020

Community Treasure Hunt

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

Start Hunting!