Extract data from multiple excel files
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I am trying to extract data from a specific column from multiple excel files and save this columns in one table
D = 'C:\Users\Desktop\Analyze\';
for k=1:250
file = sprintf('Data_%d.xlsx',k);
filename = strcat('C:\Users\TR8E1MQ\Desktop\Analyze\',file);
x = xlsread(filename);
T = x(:,7);
end
This how I am extracting one column. I want to keep those and keep adding more values from other excel files.
채택된 답변
Guillaume
2020년 1월 10일
I would recommend you use fullfile instead of strcat to build paths. This way you don't have to worry about path separators.
Assuming that all files have the number of rows, this is how I'd do it:
source_folder = 'C:\Users\Desktop\Analyze'; %much more meaningful name than D
numfiles = 250;
data = []; %will be preallocated upon reading the first file, once we know the size of a column
for fileidx = 1:numfiles
filedata = readmatrix(fullfile(source_folder, sprintf('Data_%d.xlsx', fileidx)));
if fileidx == 1
data = zeros(size(filedata, 1), numfiles); %preallocate to the height of columns x num files
end
data(:, fileidx) = filedata(:, 7); %copy column 7 into output matrix
end
end
댓글 수: 10
Assumming all the files have the same number of rows?
Yes, since all columns of a matrix must have the same number of rows.
Otherwise, you'd have to pad the shorter rows / crop the longer ones / not use a matrix for storage. Any of these is possible.
What is the other possibility for storage, in case of not using a matrix
A cell array, but be aware that a cell array is more complicated to use.
So, have your files got the same number of rows or not?
No, all of the have different number of rows.
In that case:
source_folder = 'C:\Users\Desktop\Analyze'; %much more meaningful variable name than D
numfiles = 250;
data = cell(1, numfiles); %since all files have different number of rows. Store the desired columns in a cell array
for fileidx = 1:numfiles
filedata = readmatrix(fullfile(source_folder, sprintf('Data_%d.xlsx', fileidx)));
data{fileidx} = filedata(:, 7); %copy desired column in corresponding element of the cell array
end
Is not working.
I received zha follwing error:
Undefined function or variable 'readmatrix'.
filedata = readmatrix(fullfile(source_folder, sprintf('Data_%d.xlsx', fileidx)));
For versions prior to R2019a, replace readmatrix by xlsread. Rest of the code would be the same.
Thanks. You were right. Now, I have different cells and on each of them all of my values of that specific column are saved.
I would like to analyze these cells.
-Plot in a graph all of these values and find max. , min. and average values
How can I do this?
What is the code if I wanna take multiple columns of each excel file in the same way?
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
