Add all values from loop to an array

조회 수: 3 (최근 30일)
Kyle
Kyle 2023년 5월 9일
댓글: Cris LaPierre 2023년 5월 10일
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
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
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
Kyle
Kyle 2023년 5월 10일
When I try to run this it says
"Conversion to double from cell is not possible.
Error (line 27)
outDat(i,:) =[dat(3,4) dat(3,10) dat(4,4) dat(4,11)]; "
Jon
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
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
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

댓글을 달려면 로그인하십시오.


Kyle
Kyle 2023년 5월 10일
% This is a solution but I do not think it is the most efficent
fds = fileDatastore('*.xls', 'ReadFcn', @importdata);
fullFileNames = fds.Files;
numFiles = length(fullFileNames);
o = 1;
DA = zeros(10000,4);
DA2 = num2cell(DA);
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;
DA2(o,:) = dat;
o = o +1;
end
o = o +1;
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by