Add all values from loop to an array
조회 수: 3 (최근 30일)
이전 댓글 표시
I am trying to read all xls files from a folder and display information from certain cells of each file. I want to put all of these values into a single array.
%% Problem 1
fds = fileDatastore('*.xls', 'ReadFcn', @importdata); %I believe that how this function is set up only data on matlab online will be read, function will have to be altered for downloaded matlab.
fullFileNames = fds.Files;
numFiles = length(fullFileNames);
for k = 1 : numFiles
fprintf('Now reading file %s\n', fullFileNames{k});
[status,sheets] = xlsfinfo(fullFileNames{k});
sheet = string(sheets);
S = length(sheets);
i=1;
for x = i:S
p = sheet(1,i);
[~,~,dat]=xlsread(fullFileNames{k},p);
dat=[dat(3,4) dat(3,10) dat(4,4) dat(4,11)];
i= i+1;
end
end
댓글 수: 1
Cris LaPierre
2023년 5월 10일
There is nothing about your code that is unique to MATLAB Online. You just need to be sure the first input to filedatastore is pointing to your intended folder. Currently, it will load all xls files in your current folder.
답변 (3개)
Jon
2023년 5월 9일
Let's say you want to store the values you have selected from each sheet in an array called outDat, you could modify your code to do something like
S = length(sheets);
outDat = zeros(4,S); % preallocate array to hold output data
i=1;
for x = i:S
p = sheet(1,i);
[~,~,dat]=xlsread(fullFileNames{k},p);
% store the values for this sheet in the output array
outDat(i,:) =[dat(3,4) dat(3,10) dat(4,4) dat(4,11)];
i= i+1;
end
end
댓글 수: 2
Jon
2023년 5월 10일
The problem is you are using the third argument of xlsread, which returns a cell array. If all of your data is numeric you can just get the data returned in the first argument to xlsread, which is a numeric (double) array.
Otherwise you need to convert the data from cell to matrix before putting it into the output array.
xlsread is now outdated, you can probably use readmatrix instead.
Please attach one of your excel files so I can try too and give you further advice
Cris LaPierre
2023년 5월 9일
I would use a filedatastore to load all the data into a single variable. You can see an example of how to use one to do this in this video from the Data Processing with MATLAB specialization on Coursera.
Here is the final code from that example. You can modify this to work for your data.
flightsDataStore = fileDatastore("flights*.csv","ReadFcn",@importFlightsData,"UniformRead",true);
flightsAll = readall(flightsDataStore)
Once complete, all the data from all files matching the pattern "flights*.csv" are loaded into the variable flightsAll.
댓글 수: 1
Cris LaPierre
2023년 5월 10일
Your code does not take advantage of your fileDataStore. Modifying your code, I would think you could do something like this (note this is untested since you have not shared any files for us to test with)
fds = fileDatastore('*.xls', "ReadFcn",@importMyData,"UniformRead",true);
DA = readall(fds)
function data = importMyData(filename)
sheets = sheetnames(filename);
data = zeros(length(sheets),4);
for s = 1:length(sheets)
dat = readtable(filename,'Sheet',s,'NumHeaderLines',0);
data(s,:) = [dat{3,[4,10]} dat{4,[4,11]}];
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!