Read 2 specific columns in excel file

10 views (last 30 days)
Vlatko Milic
Vlatko Milic on 9 Jan 2018
Commented: dpb on 9 Jan 2018
Hi, I am quite new to Matlab and have a question about reading specific columnws from excel files. I am interested in column A and G. There is data in the other columns but I am not interested in it. The code follows:
ImportedData{fol_no,file_no} = readtable(char(fullfile(fileInfo(1).folder,fileNames(file_no))));
el=table2array(ImportedData{fol_no,file_no}(:,1,:,7));
I get a message consisting of: You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a row subscript and a variable subscript.
I can import one of the columns, either A or G, but not both simultaneously. I guess the problem is connected to "table2array" as the array only deals with one column?
So my question to you is- how can I import the columns simultenously? Pleaste note that I am performing this procedure for a no.of files

Accepted Answer

dpb
dpb on 9 Jan 2018
In the addressing expression (:,1,:,7) there are four dimensions, not just two -- the first colon will reference all rows, then 1,:,7 looks like another 3D subscript. Try
el=table2array(ImportedData{fol_no,file_no}(:,[1 7]);
I'd suggest recasting the loop however to process each file in sequence after reading it instead of storing the input data into an array of tables. You don't outline the requirements but if each file is independent of the others then there's no need to keep all; if you do need to build a full set from the files then you should again just read each file and append the next set onto the previous, still not needing an array for the initial input.
More details of what are doing next would lead to more specific answers in that regards.
  3 Comments
dpb
dpb on 9 Jan 2018
VM Answer moved to Comment -- dpb
The files are independent by each other, in fact they all have the same structure. They are located in subfolders where the no.of excel files in each folder varies from the other. Each subfolder represents an adress, and for each Adress I want to sum annual data from the excel files. Therefore, the next step is to sum annual data within each subfolder for all Excel files within the subfolder. That was a clarification of the problem :) moreover, why I need to differentiate between the various cells in the column with 1s or 2s is because the 1s represent monthly data, and the 2s daily data. And it is the monthly data I am interested in

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!