由于我是初学者,你们这里真的很有帮助我使用matlab。现在我对matlab中的gui有所了解。我有个问题。我有一个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
答案 0 :(得分:4)
您显然已经知道INPUTDLG对话框(因为您在上面使用它),所以我很惊讶您还没有遇到UIGETDIR对话框(或{{3} } 对于这个问题)。您可以使用它来让用户浏览并选择一个文件夹,如下所示:
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
请注意,我使用函数any of the others来构建文件路径,该路径会自动处理您指定平台所需的FULLFILE。