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
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...
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!