Import file excel with different length

10 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-5-16
编辑:dpb 2020-5-17
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 2020-5-18
编辑:Fredic 2020-5-18
ok thanks, I will try it

请先登录,再进行评论。

回答(1 个)

ChrizzzlP
ChrizzzlP 2020-5-7
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 个评论
ChrizzzlP
ChrizzzlP 2020-5-7
for i = 1:size(data,1), sorry forgot the i = part
Fredic
Fredic 2020-5-7
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});

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by