How to fix error done by pressing "Cancel" button

22 ビュー (過去 30 日間)
Jethro
Jethro 2012 年 1 月 18 日
コメント済み: Titus Edelhofer 2023 年 11 月 28 日
Hi everybody!
I wrote a simple function to save my axes using saveas funct. but if I click on "Cancel" in the opened window, I have an error in my Matlab... There's a way to interrupt the operation of saving if users press "Cancel" without an error??

採用された回答

Titus Edelhofer
Titus Edelhofer 2012 年 1 月 18 日
Hi,
the uiputfile dialog returns 0 for the filename if the user presses cancel:
filename = uiputfile;
if filename==0
% user pressed cancel
return
end
% go on with saving your data ...
Titus
  4 件のコメント
Ivan Demec
Ivan Demec 2023 年 11 月 28 日
Your answer breaks the code if you use the multiselect option of the UIgetFile i.e.,
Undefined function 'eq' for input arguments of type 'cell'.
@Walter Roberson your solution works with multiselect. Thank you
Titus Edelhofer
Titus Edelhofer 2023 年 11 月 28 日
Interesting coincidence: I wrote the answer a decade ago, and most often still use the filename==0 test. Believe it or not, just about 2 weeks ago I stumbled across my own solution not working (anymore) in case of multiselect :)

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 1 月 18 日
Sorry, Cancel in which window exactly? And do you mean Cancel or do you mean the window close button? saveas() itself does not have a Cancel. If you are referring to something like a callback initiated from a window, have you considered setting the Cancel button to 'enable', 'off' during the time the saveas operation is going on?

Jethro
Jethro 2012 年 1 月 18 日
I write you my code, maybe it can help you to understand my situation...
function PB_S_G_Callback(hObject, eventdata, handles)
F=getframe(handles.G1_Grafico); %select axes in GUI
global fout
[fout path]=uiputfile({'*.png'; '.bmp'; '.jpg'; '.gif'; '.tiff'})
global fpath
fpath = strcat(path, fout);
a=saveas(gcf, fpath);
I want to use this code to save my plots (the callback name means Pushbutton to Save Plots): When opening my "save window", I can press OK (to save) or CANCEL (to cancel operation)... If I press CANCEL, Matlab shows me an error...
EDIT: I solved with Titus code, now it works correctly!!
function PB_S_G_Callback(hObject, eventdata, handles)
F=getframe(handles.G1_Grafico); %select axes in GUI
global fout
[fout path]=uiputfile({'*.png'; '.bmp'; '.jpg'; '.gif'; '.tiff'})
if fout==0
return
end
global fpath
fpath = strcat(path, fout);
a=saveas(gcf, fpath);
THANKS A LOT!
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 1 月 18 日
Yes, always test the output of uiputfile(), uigetfile(), uigetdir(), uiputdir()

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

カテゴリ

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