Reputation: 213
(http://s1273.photobucket.com/user/Chethan_tv/media/CBIR_zpsb48bce14.jpg.html)
Above image is my final output, open
push button is used to view image displayed on respective axes.
I've used following code for displaying
function open1_Callback(filename, hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)
Where, 6 is axes number. But I'm getting error like
undefined variable handles
To display image on axes I've used following code
function displayResults(filename,hObject, eventdata, handles)
% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
guidata(hObject,handles)
filename is a text file. HOW TO DISPLAY RESPECTIVE IMAGE USING PUSHBUTTON?
Upvotes: 0
Views: 690
Reputation: 1672
This is because you messed the code generated by GUIDE. Normally callback functione definition looks like this:
function SomeButton_Callback(hObject, eventdata, handles)
But in your code you write
function open1_Callback(filename, hObject, eventdata, handles)
But guide still sends three arguments to the callback function (hObject
, eventdata
, and handles
) in this specific order. So MatLab gets confused and throws an error.
You better put your filename into handles
structure in *_OpeningFcn
function and then use it from there in all calbacks.
At the end of your *_OpeningFcn
you should add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function of your button
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
And yes, all the files you open with fopen
function should be closed with fclose
. You will learn it hard way when you'll not be able to update your file in your favorite editor because another programs is using it.
See also (Making universal variables in MATLAB GUI)
UPDATE to reflect discussion in comments:
To achieve the behavior you want I'd do the following:
At the end of your *_OpeningFcn
add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
% store file name and image itself for later use
handles.MyData.Images{N} = rgb;
handles.MyData.FileNames{N} = imagename;
% we have buttons like open1 open2 open3 etc...
% add some info to the open buttons
% so they will be aware which image they display
btn = handles.(sprintf('open%d', N));
set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function for your open button do the following
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
N = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
Basically, this callback is universal: it should work without modifications for all your open buttons. You may even change callback function of open2
, open3
... buttons in GUIDE to open1_Callback
.
Upvotes: 1