Image is displaying in different axes
4 views (last 30 days)
Show older comments
Hi, I have a problem dispalying an image in a certain axes(axes4, btw). Everytime I click a certain option in the pop-up menu/list it always displays in axes5(additionally, it always displays on axes that are newly added or created, in my instance its axes5). Please do help Hoiw I do this.


Here is my code snippet.....
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
str = get(hObject, 'String');
val = get(hObject, 'Value');
% set current filter to the user-selected filter
switch str{val};
case 'RED'
a = imread('C:\Users\Vins\MATLAB_SAMPLE_Folder\Matlab\contrast.jpeg');
b = imagesc(a(:,:,1));
image(handles.axes4, b)
axis(handles.axes4, 'image', 'off');
case 'BLUE'
case 'GREEN'
end
0 Comments
Answers (1)
Deepak
on 9 Jan 2025
We can display an image in a specific axes (axes4) based on a user selection from a popup menu by creating a GUI with an axes and a popup menu using MATLAB App Designer or GUIDE. The GUI should include options for different color channels (e.g., RED, BLUE, GREEN). In the callback function of popup menu, we read the image and extract the selected color channel. By setting "axes4" as the current axes using the "axes" function, we ensure that the "imagesc" function displays the image in the correct location, allowing dynamic updates based on user interaction.
Below is the sample MATLAB code to achieve the same:
function myImageDisplayApp
% Create a figure
hFig = figure('Name', 'Image Display App', 'NumberTitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none', 'Position', [100, 100, 600, 400]);
% Create axes4
handles.axes4 = axes('Parent', hFig, 'Units', 'normalized', ...
'Position', [0.1, 0.3, 0.35, 0.6]);
% Create popupmenu2
handles.popupmenu2 = uicontrol('Style', 'popupmenu', ...
'String', {'Select', 'RED', 'BLUE', 'GREEN'}, ...
'Units', 'normalized', ...
'Position', [0.5, 0.8, 0.3, 0.1], ...
'Callback', @(hObject, eventdata) popupmenu2_Callback(hObject, eventdata, handles));
% Callback function for popupmenu2
function popupmenu2_Callback(hObject, ~, handles)
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Only proceed if a valid color is selected
if val > 1
% Load the image
a = imread('C:\Users\dchangoi\Downloads\image1.JPG');
% Select the color channel based on user selection
switch str{val}
case 'RED'
channel = a(:,:,1);
case 'BLUE'
channel = a(:,:,3);
case 'GREEN'
channel = a(:,:,2);
end
% Display the selected channel in axes4
axes(handles.axes4); % Set the current axes to axes4
imagesc(channel); % Display the selected color channel
axis image off; % Adjust axis properties
end
end
end
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!