Import file excel with different length
Show older comments
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
dpb
on 7 May 2020
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)?
Fredic
on 7 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?
Fredic
on 7 May 2020
Fredic
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.
Fredic
on 16 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...
Answers (1)
ChrizzzlP
on 7 May 2020
0 votes
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
Fredic
on 7 May 2020
ChrizzzlP
on 7 May 2020
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
Fredic
on 7 May 2020
ChrizzzlP
on 7 May 2020
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)
Fredic
on 7 May 2020
ChrizzzlP
on 7 May 2020
for i = 1:size(data,1), sorry forgot the i = part
Fredic
on 7 May 2020
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!