save button condition?

2 visualizzazioni (ultimi 30 giorni)
researcher
researcher il 2 Mag 2011
Hi i have created a gui in MATLAB. now i want to put validation on the save button such that when the user saves and if the name already exists then he should be prompted to change!How can i do this?

Risposte (2)

Matt Fig
Matt Fig il 2 Mag 2011
You don't mention what it is that you are protecting, a variable or file for example, so I will speak generally.
R = get_name_user_wants;
while exists_R
Tell_user_this_already_exists;
R = get_name_user_wants;
end
Where get_name_user_wants is your method of getting the information from the user, like a dialog box or examining the string of a uicontrol. exists_R is your method of checking, depending on what it is you are trying to save. Here I imagine you will want to use the EXIST function in some way. Tell_user_this_already_exists is your way of notifying the user that the name already exists. This could involve a warn dialog or setting the string in the uicontrol to a red message for a second, etc....
%
%
%
%
EDIT In response to your 'answer' below.
What is 'exists_image' in your code? I said above that you will want to use the EXIST function.
button = questdlg('Do you want to save the changes?')
if strcmp('Yes',button)
% image =(IMG) %define a 8-bit unsigned integer.
[filename,pathname] = uiputfile({'*.jpg', 'JPEG Image File'; ...
'*.*', 'All Files (*.*)'}, ...
'Save current image as');
while exist(filename,'file')
uiwait(errordlg(['File already exists!',...
' Choose another Name.'],'!Error!'));
[filename,pathname] = uiputfile({'*.jpg', 'JPEG Image File'; ...
'*.*', 'All Files (*.*)'}, ...
'Save current image as');
end
% Here is where you save with filename!
end
Also note that, unless this functions is a nested function to a function which has defined IMG, there will exist no variable named IMG, so the line referencing IMG will error. That is why I commented it out. Another thing which you should take care to change: Your callback is named SAVE. But this is a built-in MATLAB function. Therefor, your callback will mask the built-in function so that you will not be able to use MATLAB SAVE anywhere in the scope of your function..
However, your question pertaining to the filename has been answered.
  1 Commento
researcher
researcher il 3 Mag 2011
I tried using this but i think there was some problem in my assignmnet of R.

Accedi per commentare.


researcher
researcher il 3 Mag 2011
hi the user will save an image each time he clicks the save button. this is what my code looks like.however it does not give the desired result.
function save(hObject, eventdata, handles)
button = questdlg('Do you want to save the changes?')
switch button
case {'No'}
% take no action
case 'Yes'
image =(IMG) %define a 8-bit unsigned integer.
[filename,pathname] = uiputfile( ...
{'*.jpg', 'JPEG Image File'; ...
'*.*', 'All Files (*.*)'}, ...
'Save current image as');
while exists_image
A = exist('name','.jpg')
if(A==1)
warndlg('This name already exists in the current directory!','!! Warning !!')
else
%save
%Open standard dialog box for saving files, displays a dialog box titled 'Save
%current image as’ with the filter specifier set to .jpg file, JPEG Image file and all
%files.
var=strcat(pathname,filename,'.jpg')
%horizontally concatenates corresponding rows of the character arrays pathname, filename and ‘.jpg’.
imwrite(image,var), %write the image to pc in the format specified by var
end
end
end
end

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by