how can I use sheet name as plot title in multiple sheet excel file?

5 ビュー (過去 30 日間)
Saeedullah Mandokhail
Saeedullah Mandokhail 2016 年 1 月 17 日
編集済み: Walter Roberson 2018 年 3 月 13 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 3 月 12 日
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
noname 2018 年 3 月 13 日
編集済み: Walter Roberson 2018 年 3 月 13 日
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

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 17 日
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 1 月 17 日
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
Saeedullah Mandokhail
Saeedullah Mandokhail 2016 年 1 月 18 日
Walter Roberson, It works well.I really appreciate your help. Thank you very much.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by