Import file excel with different length

조회 수: 10 (최근 30일)
Fredic
Fredic 2020년 5월 7일
편집: Fredic 2020년 5월 18일
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