Adding plot titles based on excel file name in a for loop

Hi, I have a bunch of excel files that I have looped through matlab. I am able to read the files into matlab and create the graphs I want, but I would like the titles of the graphs to be the same as the excel file they came from. I am unclear how to have the title of each individual plot reflect the file name. This is my script so far:
files = dir('../../Data/USGS/USGS_StrSites/Excel_files/*.xls');
files = [files;dir('../../Data/USGS/USGS_StrSites/Excel_files/*.xls')];
for i =1:length(files(:,1))
filepath=strcat('../../Data/USGS/USGS_StrSites/Excel_files/',files(i,1).name);
%filepath=strcat('../Data/',files(i,1).name);
R = readtable(filepath);
[m,n]=size(R);
Rcell=table2cell(R);
Wcell=(Rcell(:,2:n));
WM= cell2mat(Wcell)
%Plot of discharge and temp over time
figure(i);
[ax,h1,h2] = plotyy(D,WM(:,2),D,WM(:,3),'plot');
title()
end
end
Thank you!

回答 (1 件)

Star Strider
Star Strider 2017 年 7 月 13 日

0 投票

Use the fileparts function to get the file name:
...
[~,filename] = fileparts(filepath);
figure(i);
[ax,h1,h2] = plotyy(D,WM(:,2),D,WM(:,3),'plot');
title(filename)
NOTE I cannot run your code to test this, but it should work.

2 件のコメント

MegE
MegE 2017 年 7 月 13 日
I ran this change to the script, but it simply changes the title of all the graphs to "filename"
Star Strider
Star Strider 2017 年 7 月 15 日
The ‘filename’ variable should be the name of the file you specified in your ‘filepath’ variable. My code worked for me when I tested it.
Check to see what your ‘filepath’ file names are.
Also, you may not be runnning my code correctly.
This is correct:
title(filename)
This is not correct:
title('filename')

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

カテゴリ

タグ

質問済み:

2017 年 7 月 12 日

コメント済み:

2017 年 7 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by