Shinobii
Shinobii

Reputation: 2561

MATLAB save image from GUI using push button

I want to save an image from a designated axes. I keep getting You may not have permission to write error. Here is my code for the save as push button:

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

Also, handles.TReg is a transformed image defined on another function.

I cannot seem to find my error here, any thoughts would be appreciated.

Edit If I use the code:

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

The file will save as Name.bmp in the proper directory. However I did notice that when I try saving with the original code, the error also reads (I missed this bit sorry):

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

I think the pathname should read like

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

could this be the problem?

Upvotes: 0

Views: 14745

Answers (3)

Amro
Amro

Reputation: 124563

Small mistake:

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

Also its a good idea to check that the user didn't cancel the dialog box:

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

Upvotes: 2

Shinobii
Shinobii

Reputation: 2561

Again, it seems like a silly mistake I should have found earlier.

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

In Name I needed to flip the FileName and PathName.

Thanks for your help!

Upvotes: 0

Herr von Wurst
Herr von Wurst

Reputation: 2621

Check the path and the filename and try to save an image by calling imwrite by hand. This probably has nothing to do with the GUI and the button callback but with either the file permissions and/or the path name.

Upvotes: 1

Related Questions