Diplay a dialog box when using a wrong callback using images in gui

I'm still learning how tu use GUI in matlab. i Have a gui that reads an image and display it. Then i have 3 buttons which converts the image into a grayscale image (R, G or B) then, using a slider, i set the threshold for a binary image. Well, when i use the slider without pressing any buttons (r, g or b) i get an error " Reference to non-existent fiel". And that's fine, i know i have to get an error there, but what i want to do is to display an error using waitfor(msgbox('')); when that error occurs. i'm still traying to do that, but i need a little help please.
Well, i want to use this gui when running a certain part from a code in a .mat file. For example. I run the code, read an image, then call the gui, binarize the same image, save the binarized image, and then use that saved image in the original code and continue the code. Hope it makes sense. How can i do this?
Here is the gui
function prueba_gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.imagen = imread('G:\Memoria\test\Imagen125.jpg');
axes(handles.axes1);
imshow(handles.imagen);
guidata(hObject, handles);
function varargout = prueba_gui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function binar_Callback(hObject, eventdata, handles)
handles.output = hObject;
axes(handles.axes1);
tempImg = handles.im;
thresholdValue = get(handles.binar, 'value');
binaryImage = tempImg> thresholdValue;
se = strel('square',4);
w = imdilate(binaryImage,se);
imshow(w);
guidata(hObject,handles);
function binar_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function RED_Callback(hObject, eventdata, handles)
handles.output = hObject;
axes(handles.axes1);
handles.im = handles.imagen(:,:,1);
imshow(handles.im);
guidata(hObject,handles);
function GREEN_Callback(hObject, eventdata, handles)
handles.output = hObject;
axes(handles.axes1);
handles.im = handles.imagen(:,:,2);
imshow(handles.im);
guidata(hObject,handles);
function BLUE_Callback(hObject, eventdata, handles)
handles.output = hObject;
axes(handles.axes1);
handles.im = handles.imagen(:,:,3);
imshow(handles.im);
guidata(hObject,handles);
% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
handles.output = hObject;
%nothing here yet

 Respuesta aceptada

Image Analyst
Image Analyst el 25 de Mayo de 2014
See the FAQ for how to share variables between different functions in your m-file. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

9 comentarios

sorry, can you be more especific please? i understand when the variable is a number or an array, but in this case my variable is an image.
It doesn't matter - they're all variables. Why would you get an error like you said? Can you post the exact error message - all the red text?
Alejandro
Alejandro el 26 de Mayo de 2014
Editada: Alejandro el 26 de Mayo de 2014
this error:
Reference to non-existent field 'im'.
Error in prueba_gui>binar_Callback (line 74)
tempImg = handles.im;
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in prueba_gui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)prueba_gui('binar_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
i actually know why that error occurs. The normal image (RGB image) is not defined in binar_callback. All i want to do is to tell the "user", that before using the slider to set the thresholdValue, to press the RED, GREEN or BLUE buttons and then use the slider. And if the "user" use the slider before pressing any of those buttons, display an error in a msgbox saying that a button needs to be press first (or the image has to be grayscale). I can't figure it out yet.
% See if we can pull out the im field.
hasField = isfield(handles, 'im');
if ~hasField
% Has no im field yet.
uiwait(warndlg('You need to click the binar button first!'));
return;
end
i added this:
hasField = isfield(handles, 'im');
if ~hasField
% Has no im field yet.
uiwait(warndlg('You need to click the binar button first!'));
return;
else hasField = handles.im;
end
and it works!! Thanks!! could you help me with the second question i asked please?
I did. That is what the link was for. Just attach the variable to the handles structure or use setappdata() and get appdata(), or declare it global.
Please mark the answer as Accepted if we're done here. Thanks.
a ok, no problem. But i was asking for using the saved binarized image in a *.mat file that is the general code for my program. The gui is just for binarize an image, and i want to use this image in further processes in the code. that´s all i need for now.
You can save it in a mat file. That's one way listed in the FAQ. Though it's slower because you have to write to disk and read from disk. It's mostly used for passing data between two separate GUIs rather than within functions of the same GUI.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Mayo de 2014

Comentada:

el 2 de Jun. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by