Read 2 specific columns in excel file

조회 수: 31 (최근 30일)
Vlatko Milic
Vlatko Milic 2018년 1월 9일
댓글: dpb 2018년 1월 9일
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

채택된 답변

dpb
dpb 2018년 1월 9일
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
dpb
dpb 2018년 1월 9일
편집: dpb 2018년 1월 9일
That's a trivial usage of logical addressing; the bigger question still revolves about whether these sums are to be computed on a file at at time or over all files (albeit if they're just sums then a running total is mathematically the same as the sum of sums).
Either way, it seems there's no need to do anything but process the data as you read each file -- unless there's more to be done yet than outlined.
dpb
dpb 2018년 1월 9일
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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by