【问题标题】:How the user can save the gui(result) in other folder?用户如何将 gui(结果)保存在其他文件夹中?
【发布时间】:2011-07-01 09:05:36
【问题描述】:

因为我是初学者,所以你们在这里真的帮助我使用 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文件夹中。我想让用户如何选择他想将 gui 保存在 .bmg 文件中的文件夹??

我将此编码用作@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

【问题讨论】:

  • 发生错误是因为在下面的示例中,我在驼峰式中使用名称fileName,而您使用全部小写的filename

标签: user-interface matlab save


【解决方案1】:

您显然已经知道INPUTDLG 对话框(因为您在上面使用它),所以我很惊讶您还没有遇到UIGETDIR 对话框(或any of the others)。您可以使用它让用户浏览并选择一个文件夹,如下所示:

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 来构造文件路径,它会自动处理给定平台所需的file separators

【讨论】:

  • 我认为 directoryname==0 不起作用,因为它在上面出现错误。
猜你喜欢
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2020-02-15
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多