how to use a value selected in listbox to use by a push button
2 次查看(过去 30 天)
显示 更早的评论
i have a gui which contains
- a push button to load file
- a list box to select no of color (like 8,16,32)
- a push button to convert the loaded rgb image to indexed using the value selected from the list box for its colormap.
- an axes to display the image
how can i use the value of of listbox in my push button callback ??
[Merged information from Answer]
index_selected = get(hObject,'Value');
list = get(hObject,'String');
item_selected = list{index_selected};
handles.item = item_selected;
guidata(hObject, handles);
im using this in listbox call back but handles.item is putting the integer value in single quotation marks ('8') like this which my rgb2ind function doesnt accept and shows an error "X must be a positive integer value."
code for pushbutton callback
noofcol = handles.item;
[indimage,indmap] = rgb2ind('loaded file',noofcol);
0 个评论
采纳的回答
更多回答(1 个)
Image Analyst
2012-6-24
You're using braces instead of parentheses. Here's one way to do it. (You can cut out the switch statement if you're not going to use videos, and replace the whole thing with a simple call to imshow.)
%=====================================================================
% --- Executes on clicking in lstImageList listbox.
% Display image from disk and plots histogram
function lstImageList_Callback(hObject, eventdata, handles)
% hObject handle to lstImageList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns lstImageList contents as cell array
% contents{get(hObject,'Value')} returns selected item from lstImageList
% Get item number
Selected = get(handles.lstImageList, 'value');
% If more than one is selected, bail out.
if length(Selected) > 1
baseImageFileName = '';
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow')
drawnow; % Cursor won't change right away unless you do this.
return;
end
% If only one is selected, display it.
set(handles.axesPlot, 'visible', 'off'); % Hide plot of results since there are no results yet.
set(handles.btnDrawNewMask, 'enable', 'on'); % Enable Draw Mask Button.
% Get all the item names
ListOfImageNames = get(handles.lstImageList, 'string');
% Get just the one that was selected.
baseImageFileName = strcat(cell2mat(ListOfImageNames(Selected)));
fullImageFileName = [handles.ImageFolder '/' baseImageFileName]; % Prepend folder.
[folder, baseFileName, extension] = fileparts(fullImageFileName);
switch lower(extension)
case {'.mov', '.wmv', '.asf'}
msgboxw('Mov and wmv format video files are not supported by MATLAB.');
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow');
drawnow; % Cursor won't change right away unless you do this.
return;
case '.avi'
% The only video format supported natively by MATLAB is avi.
% A more complicated video player plug in is on MATLAB File Central
% that will support more types of video. It has a bunch of DLL's and
% other files that you have to install.
% Read the file into a MATLAB movie structure.
myVideo = aviread(fullImageFileName);
myVideoParameters = aviinfo(fullImageFileName);
numberOfFrames = myVideoParameters.NumFrames;
% Extract a frame.
frameToView = uint8(floor(numberOfFrames/2)); % Take the middle frame.
imgFirstFrame = myVideo(frameToView).cdata; % The index is the frame number.
imshow(imgFirstFrame); % Display the first frame.
% Play the movie in the axes. It doesn't stretch to fit the axes.
% The macro will wait until it finishes before continuing.
movie(handles.axesImage, myVideo);
guidata(hObject, handles);
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow');
drawnow; % Cursor won't change right away unless you do this.
return;
otherwise
% It's a regular image. Display the image.
imshow(fullImageFileName);
end
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow');
drawnow; % Cursor won't change right away unless you do this.
guidata(hObject, handles);
return % from lstImageList_Callback()
2 个评论
Walter Roberson
2012-6-24
?? Braces were correct in the place that Rao used them. The String field of a listbox returns either a character array (less likely) or a cell array of strings (more likely); in the cell array of strings case, indexing with {} is what you want. (And in the character array case, you need to use two indices, not just one.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!