how can I use sheet name as plot title in multiple sheet excel file?
5 views (last 30 days)
Show older comments
Saeedullah Mandokhail
on 17 Jan 2016
Edited: Walter Roberson
on 13 Mar 2018
My code is running through multiple excel sheets in an excel file, and is making plots according to each sheet, also I am having many excel files to analyze. 1. I want the title to indicate the sheet name of the respective plot. 2. My excel file has 10 sheets. I want 6 figures in one page, that is two pages for one excel file using subplots. my code plots 6 figures in the first page and do not create second page for the same excel file and gives error. My code looks like this:
clc;
XLfile = dir;
number_of_files = length(XLfile);
index = 3;
for index = 3:number_of_files
filename = XLfile(index).name;
for i=1:12
x=xlsread(filename,i,'C2:C10000');
y=xlsread(filename,i,'D2:D10000');
subplot(3,2,i)
plot(x,y,'b')
xlabel('Shear strain (%)','FontSize', 8)
ylabel('Shear stress ratio (\tau^{\prime}/\sigma_{vo}^{\prime})','FontSize', 8)
title(filename(1:end-4))
set(gcf, 'PaperPosition', [0 0 5 8]);
set(gcf, 'PaperSize', [5 8]);
saveas(gcf,[filename(1:end-4),'pdf'])
end
figure
end
5 Comments
Walter Roberson
on 12 Mar 2018
You can use xlsinfo() to find the spreadsheet names. You can use menu() or questdlg() or inputdlg() to get the user to choose one. Once you have the sheet name you can xlsread() passing in the sheet name as the second parameter. You can then boxplot() the data.
noname
on 13 Mar 2018
Edited: Walter Roberson
on 13 Mar 2018
Here is what I did: I used xlsfinfo to get the spreadsheet name, and xlsread() to passing the sheetname into parameter. I just don't know how to plot the selected spreadsheet since it has different size. Can you show me in the code below? I'd really appreciate it. Thanks alot!
filename = '/Users/kn/Desktop/Test.xlsx';
[~,sheets] = xlsfinfo(filename);
num_sheets = length(sheets);
data = cell(num_sheets, 1);
for K = 1 : num_sheets
data{K} = xlsread(filename, sheets{K});
end
Accepted Answer
Walter Roberson
on 17 Jan 2016
3 Comments
Walter Roberson
on 17 Jan 2016
XLfile = dir();
XLfile([XLfile.isdir]) = []; %get rid of directories including . and ..
number_of_files = length(XLfile);
for index = 1:number_of_files
filename = XLfile(index).name;
[status, sheetnames] = xlsfinfo(filename);
if isempty(status)
warning( sprintf('Skipping non-excel file: "%s"', filename);
continue;
end
fig = figure();
nsheet = min( length(sheetnames), 12 ); %ignore past 12
if nsheet < 12
warning( sprintf('Only %d sheets in file "%s"', nsheet, filename);
end
for i = 1:nsheet
sheetname = sheetnames{i};
x = xlsread(filename, sheetname, 'C2:C10000');
y = xlsread(filename, sheetname, 'D2:D10000');
ax = subplot(6, 2, i, 'Parent', fig);
plot(ax, x, y, 'b');
xlabel(ax, 'Shear strain (%)','FontSize', 8);
ylabel(ax, 'Shear stress ratio (\tau^{\prime}/\sigma_{vo}^{\prime})','FontSize', 8);
title(ax, sheetname);
end
[~, basename, ~] = fileparts(filename);
set(fig, 'PaperUnits', 'inch', 'PaperPosition', [0 0 5 8], 'PaperSize', [5 8]);
saveas(gcf, [basename '.pdf'])
end
More Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!