How can I set the file name as my title name in a subfolder for numerous plots

2 ビュー (過去 30 日間)
Vlatko Milic
Vlatko Milic 2018 年 5 月 23 日
コメント済み: Ameer Hamza 2018 年 5 月 24 日
Hi,
I am trying to set the title name (that varies depending on the plot) as the file name in a subfolder. I get the error code: Error using sprintf. Function is not defined for 'cell' inputs.
So there is some mistake with my syntax. I have tried different approaches but I don't get it to work. My code is as follows:
figure Timestep=(1:length(d_f_t(:,1)))'; plot(Timestep,W_per_K_serie, '*'); title(['Byggnad', subfolderNames(b)])
folder_path='PathXXX';
saveas(fullfile(folder_path, sprintf('%s.jpg',get(get(gca,'title'),'string'))));
Does anybody have any advice? Thank you in advance
  2 件のコメント
Paolo
Paolo 2018 年 5 月 23 日
編集済み: Paolo 2018 年 5 月 23 日
I tried your code as follows:
figure
title(['Byggnad'])
folder_path='PathXXX';
mkdir(folder_path);
saveas(gca,fullfile(folder_path, sprintf('%s.jpg',get(get(gca,'title'),'string'))));
And it works fine for me. You must specify the handle to the figure when using saveas.
Vlatko Milic
Vlatko Milic 2018 年 5 月 23 日
Hi,
It doesnt't work still unforuntately. My error is now:' Error using saveas. Invalid handle'.
The code looks as follows:
folder_path='XXXXX'; mkdir(folder_path);
tmp = char(get(get(gca,'title'),'string')); saveas(fighandle, fullfile(folder_path, sprintf('%s.jpg', tmp)));
It is noteworthy that my title changes name for every loop I'm performing. However, this should not affect the result? Thank you in advance.

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

採用された回答

Stephen23
Stephen23 2018 年 5 月 23 日
編集済み: Stephen23 2018 年 5 月 24 日
The title 'String' property may be a cell array, string array, or a char vector. You can easily convert this into a char vector:
tmp = char(get(get(gca,'title'),'string'));
saveas(figH, fullfile(folder_path, sprintf('%s.jpg',tmp)));
I would recommend that you avoid gca, gcf, etc., and always obtain and use explicit graphics handles for all graphics objects that you need to refer to.
  7 件のコメント
Stephen23
Stephen23 2018 年 5 月 24 日
@Ameer Hamza: thank you for the comment, I fixed the answer.
Ameer Hamza
Ameer Hamza 2018 年 5 月 24 日
@Stephen, you are welcome.

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

その他の回答 (1 件)

Vlatko Milic
Vlatko Milic 2018 年 5 月 23 日
Yes, I am calling on the function with plot. I got it to work with gca instead of fighandle. However, some parts of the text in the title got misarranged in the jpg file. That I don't understand. Thank you!

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by