poster
2019-12-14, 20:13
因为我是初学者,所以在座的各位真的可以帮助我使用matlab。现在我对Matlab中的gui有所了解。我有个问题。我有一个图形用户界面和一个保存按钮。我做了这个编码:
filename = inputdlg('Please enter the name for your figures'); extensions = {'fig','bmp'}; for k = 1:length(extensions saveas(gcf, filename{:}, extensions{k}) set(gcf,'PaperPositionMode','auto') end 但这只能保存在我的GUI文件夹中。我要如何使用户可以选择他要在.bmg文件中保存gui的文件夹?
我将此编码用作@gnovice建议的编辑内容:
filename = inputdlg('Please enter the name for your figures'); extensions = {'fig','bmp'}; directoryName = uigetdir; %# Open directory-selection dialog if directoryName == 0 %# User pressed the "Cancel" button... directoryName = ''; %# ...so choose the empty string for the folder end saveas(gcf,fullfile(directoryName,fileName),extension); %# Save to the folder set(gcf,'PaperPositionMode','auto') 但是发生了此错误:
??? Error while evaluating uipushtool ClickedCallback ??? Undefined function or variable 'fileName'. Error in ==> fyp_editor>uipushtool9_ClickedCallback at 1605 saveas(gcf,fullfile(directoryName,fileName),extension); %# Save to the folder Error in ==> gui_mainfcn at 96 feval(varargin{:}); Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:}); Error in ==> @(hObject,eventdata)fyp_editor('uipushtool9_ClickedCallback',hObject,eventdata,guidata(hObject)) ??? Error while evaluating uipushtool ClickedCallback
回答:
您显然已经知道了INPUTDLG (http://www.mathworks.com/help/techdoc/ref/inputdlg.html)对话框(因为您在上面使用了它),所以令您感到惊讶的是您还没有遇到UIGETDIR (http://www.mathworks.com/help/techdoc/ref/uigetdir.html)对话框(或与此相关的任何其他 (http://www.mathworks.com/help/techdoc/ref/f16-40727.html#f16-8026)对话框)。您可以使用它来让用户浏览并选择一个文件夹,如下所示:
fileName = inputdlg('Please enter the name for your figures'); directoryName = uigetdir('','Please select a folder to save to'); if directoryName == 0 %# User pressed the "Cancel" button... directoryName = ''; %# ...so choose the empty string for the folder end filePath = fullfile(directoryName,fileName{1}); %# Create the file path extensions = {'fig','bmp'}; for k = 1:length(extensions) saveas(gcf,filePath,extensions{k}); %# Save the file set(gcf,'PaperPositionMode','auto'); end 请注意,我使用函数FULLFILE (http://www.mathworks.com/help/techdoc/ref/fullfile.html)构造文件路径,该路径会自动处理给定平台所需的文件分隔符 (http://www.mathworks.com/help/techdoc/ref/filesep.html) 。
更多&回答... (https://stackoverflow.com/questions/5067986)
filename = inputdlg('Please enter the name for your figures'); extensions = {'fig','bmp'}; for k = 1:length(extensions saveas(gcf, filename{:}, extensions{k}) set(gcf,'PaperPositionMode','auto') end 但这只能保存在我的GUI文件夹中。我要如何使用户可以选择他要在.bmg文件中保存gui的文件夹?
我将此编码用作@gnovice建议的编辑内容:
filename = inputdlg('Please enter the name for your figures'); extensions = {'fig','bmp'}; directoryName = uigetdir; %# Open directory-selection dialog if directoryName == 0 %# User pressed the "Cancel" button... directoryName = ''; %# ...so choose the empty string for the folder end saveas(gcf,fullfile(directoryName,fileName),extension); %# Save to the folder set(gcf,'PaperPositionMode','auto') 但是发生了此错误:
??? Error while evaluating uipushtool ClickedCallback ??? Undefined function or variable 'fileName'. Error in ==> fyp_editor>uipushtool9_ClickedCallback at 1605 saveas(gcf,fullfile(directoryName,fileName),extension); %# Save to the folder Error in ==> gui_mainfcn at 96 feval(varargin{:}); Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:}); Error in ==> @(hObject,eventdata)fyp_editor('uipushtool9_ClickedCallback',hObject,eventdata,guidata(hObject)) ??? Error while evaluating uipushtool ClickedCallback
回答:
您显然已经知道了INPUTDLG (http://www.mathworks.com/help/techdoc/ref/inputdlg.html)对话框(因为您在上面使用了它),所以令您感到惊讶的是您还没有遇到UIGETDIR (http://www.mathworks.com/help/techdoc/ref/uigetdir.html)对话框(或与此相关的任何其他 (http://www.mathworks.com/help/techdoc/ref/f16-40727.html#f16-8026)对话框)。您可以使用它来让用户浏览并选择一个文件夹,如下所示:
fileName = inputdlg('Please enter the name for your figures'); directoryName = uigetdir('','Please select a folder to save to'); if directoryName == 0 %# User pressed the "Cancel" button... directoryName = ''; %# ...so choose the empty string for the folder end filePath = fullfile(directoryName,fileName{1}); %# Create the file path extensions = {'fig','bmp'}; for k = 1:length(extensions) saveas(gcf,filePath,extensions{k}); %# Save the file set(gcf,'PaperPositionMode','auto'); end 请注意,我使用函数FULLFILE (http://www.mathworks.com/help/techdoc/ref/fullfile.html)构造文件路径,该路径会自动处理给定平台所需的文件分隔符 (http://www.mathworks.com/help/techdoc/ref/filesep.html) 。
更多&回答... (https://stackoverflow.com/questions/5067986)