MATLAB使用按钮从GUI保存图像

时间:2012-07-26 14:45:18

标签: matlab user-interface

我想从指定的轴保存图像。我一直收到You may not have permission to write错误。这是我的保存为按钮的代码:

axes(handles.axes3);
[FileName, PathName] = uiputfile('*bmp', 'Save As');
Name = fullfile(FileName, PathName);
imwrite(handles.TReg, Name, 'bmp');

此外,handles.TReg是在另一个函数上定义的变换图像。

我似乎无法在这里找到我的错误,任何想法都会受到赞赏。

修改 如果我使用代码:

axes(handles.axes3);
[FileName, PathName] = uiputfile('*bmp', 'Save As');
Name = fullfile(FileName, PathName);
imwrite(handles.TReg, 'Name', 'bmp');

该文件将Name.bmp保存在正确的目录中。但是我确实注意到当我尝试使用原始代码保存时,错误也会显示(我很遗憾地抱歉):

Can't open file "Image1\C:\Users\Shinobii\Documents\MATLAB\" for writing.

我认为路径名应该是

"C:\Users\Shinobii\Documents\MATLAB\Image1"

这可能是问题吗?

3 个答案:

答案 0 :(得分:2)

小错误:

[FileName, PathName] = uiputfile('*.bmp', 'Save As'); %# <-- dot
Name = fullfile(PathName,FileName);  %# <--- reverse the order of arguments
imwrite(img, Name, 'bmp');

最好还是检查用户是否取消了对话框:

[FileName, PathName] = uiputfile('*.bmp', 'Save As');
if PathName==0, return; end    %# or display an error message

答案 1 :(得分:1)

检查路径和文件名,并尝试通过手动调用imwrite来保存图像。这可能与GUI和按钮回调无关,但与文件权限和/或路径名无关。

答案 2 :(得分:0)

再次,这似乎是我之前应该发现的一个愚蠢的错误。

axes(handles.axes3);
[FileName, PathName] = uiputfile('*bmp', 'Save As');
Name = fullfile(PathName, FileName);
imwrite(handles.TReg, Name, 'bmp');

Name我需要翻转FileNamePathName

感谢您的帮助!