0

I want to write a program that is able to read a specific range of numerous excel files in a folder. because I need MATLAB to read from several excel files, I can't use a coding like this :

xlsread('Report1',1,'k41')

Is it possible to modify below codes in a way to be able to read 'K41' cellular from each excel file?

clc
clear all
Folder = 'D:\Program Files\MATLAB\R2013a\bin';
XLfiles = dir(fullfile(Folder, '*.xlsx'));
for i = 1:length(XLfiles)
data = xlsread(fullfile(Folder, XLfiles(i).name));
end 
Pedram
  • 11
  • 4

1 Answers1

0

As excaza said, xlsread should work, just check the 'range' parameter from xlsread, needs to be a string in this format (this is to import only C1).

'C1:C2'

If you use K41:K41 it imports nothing, as it is a 0 range. Maybe that is the confusion here.

See if this might work

all_cells = []; %store all your cells in here
Folder = 'D:\Program Files\MATLAB\R2013a\bin';

   XLfiles = dir(fullfile(Folder, '*.xlsx'));
   for i = 1:length(XLfiles)
     all_cells (end+1)= xlsread(fullfile(Folder, XLfiles(i).name), 'K41:K42');
   end 
ninehundred
  • 231
  • 1
  • 7
  • The first part of your answer is not correct. If you specify a sheet (as OP has done) you do not need to include a colon and opposite corner to describe a single cell. Please read the documentation for the functions you are using. – sco1 Aug 13 '15 at 14:27
  • I tried it out on my computer with this `>> data_cell = xlsread('C:\Users\User1\Documents\MATLAB\DataCleaned', 'C1:C2')` and it works. Using only 'C1' as a range gets me Worksheet not found error. If I specify the worksheet number as the second argument (as in the very first line of OPs question), giving me this `>> data_cell = xlsread('C:\Users\User1\Documents\MATLAB\DataCleaned', 1, 'C1')` , it again gives me an empty return. If I use 'C1:C2' as the last argument, I am getting cell C1's data. – ninehundred Aug 13 '15 at 14:39
  • The first one will not work because you have not specified a sheet, like I said. `xlswrite('test.xlsx', 1:5, 1); xlsread('test.xlsx', 1, 'C1')` returns `3`, as expected. – sco1 Aug 13 '15 at 14:47
  • `xlsread('some_file.xlsx','C2:C2')` works fine for me, no worksheet required, I just tried it out again. In my case, yours does not work, as somehow indexing to the first row returns nothing, and indexing to the second row returns the first row value. Your example works if I use 'C2'. Might be specific to my input data though. – ninehundred Aug 13 '15 at 16:08