Read multiple sheets from single Excel file and extract a specific set of columns from it.
4 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!