Read multiple sheets from single Excel file and extract a specific set of columns from it.

4 views (last 30 days)
Problem Statement:
I have a .xlsx file in the target folder with multiple sheets.
1) I want to read read sheet_1.
2) Extract data from columns 1 an 2 from sheet_1 and store it in an array for further processing.
read next two columns from sheet_1 and store it in an array for further processing.
repeat till I have read all the columns.
3) Analyze and plot the data.
4) Move to Sheet_2.
5) Repeat the process untill I have read and analyzed every single sheet of the .csv file.
Can some please help me figure out the solution code to this problem.
Thank You!
  8 Comments
Ameer Hamza
Ameer Hamza on 6 May 2020
There are 13 columns in your file. You asked to group the columns in 2. What to do with the 13th column?
Sagar Jagina
Sagar Jagina on 6 May 2020
Leave the first column(time) and start from second column.
For example 2-3, 4-5,....12-13.
But I was planning if it can be more generic so that I can modify as I as need.
Thank you!

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 6 May 2020
Try the following code
num_sheets = 3;
data = cell(1,num_sheets);
for i=1:num_sheets
data{i} = readmatrix('Sample.xlsx', 'Sheet', i);
end
part_data = cell(1,num_sheets); % partitioned data in 2 columns
for i=1:num_sheets
M = data{i}(:,2:end);
num_part = size(M,2)/2;
part_data{i} = mat2cell(data{i}(:,2:end), size(M,1), 2*ones(1,num_part));
end
% plotting
for i=1:num_sheets
figure;
tiledlayout('flow');
data = part_data{i}; % plotting data for i-th sheet
for j=1:numel(data)
nexttile
plot(data{j}(:,1), data{j}(:,2));
end
end
  6 Comments
Amit
Amit on 11 Mar 2023
@Ameer Hamza Thanks, this help me also. However one problem I face when I trying to plot all the figure on same axis. I try "hold on" before/after the last for loop but its not working. I am very much thankful for your help.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!