Looping through files and creating and saving plots

3 ビュー (過去 30 日間)
John
John 2013 年 1 月 28 日
Hi,
I'm trying to loop through a folder of files (20) to create 20 plots.
I have most of the code completed I think, but I am unsure of how to
1. title the plot with the name of the file from which the data came from and 2. How to save the plot as a .tif image with the name of the file
Would anybody be able to advise me on this?
Thank you
files = cellstr(ls('*.xls'));
for k = 1:length(files)
sch_cycle = xlsread(files{k}, 'Sheet1');
Y1 = sch_cycle(:,1);
createfigure(Y1);
[~,fn] = fileparts(files{k});
saveas(gca,'fn.tif') %%%-Question 2%%%
end
function createfigure(Y1)
%CREATEFIGURE3(Y1)
% Y1: vector of y data
% Auto-generated by MATLAB on 16-Jan-2013 15:53:12
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,...
'XTick',[0 200 400 600 800 1000 1200 1400 1600 1800],...
'FontWeight','bold',...
'FontSize',14);
box(axes1,'on');
hold(axes1,'all');
% Create plot
plot(Y1,'LineWidth',1);
% Create xlabel
xlabel('Time (s)','FontWeight','bold','FontSize',14);
% Create ylabel
ylabel('Velocity (km/hr)','FontWeight','bold','FontSize',14);
% Create title
title('fn','FontWeight','bold',... %%%-Question 1%%%
'FontSize',14);
  1 件のコメント
John
John 2013 年 1 月 28 日
Any help would be greatly appreciated.

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

採用された回答

Walter Roberson
Walter Roberson 2013 年 1 月 28 日
  2 件のコメント
John
John 2013 年 1 月 31 日
Hi Walter,
Is the above code incorrect to create and save the plots? I thought the first piece of code would be correct? I just wanted to know to put the file name in the title of the plot in each loop. I got the file name using fileparts but I am unsure of the correct syntax to but it in the title of the plot.
Thank you
Jan
Jan 2013 年 1 月 31 日
@John: You can simply try it, if your code does what you want. Currently it creates the same filename "fn.tif" in each iteration and the subfunction createfigure does not know the name of the file, because this has not been provided in the input arguments.

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

その他の回答 (1 件)

Jan
Jan 2013 年 1 月 31 日
編集済み: Jan 2013 年 1 月 31 日
[~,fn] = fileparts(files{k});
createfigure(Y1, fn);
saveas(gca,[fn, '.tif'])
and:
function createfigure(Y1, FileName)
...
title(axes1, FileName, 'Interpreter', 'none');
The used methods are described in the link Walter has posted.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by