IN GUI related with appending strings

I have a figure. I have some strings in Popup menu or list box. say red,green,blue. Actually I need that string selected to be displayed at the top of the figure. That is okay. The problem is when I select the next string (say green), It have to be appended with red. Displaying red,green. Next when I select the blue, It should display as red,green,blue. Please rectify the problem.
clc;
clf;
clear all;
close all;
figure;
imshow('attachment.jpg');
str = {'building','grass','mountain','sky','trees','roads','stadium'};
hL = uicontrol('style','popup','string',str,'position', [20 20 200 30],...
'callback',@(src,evt)title(sprintf('The objects are: %s',str{get(src,'value')})));

1 commentaire

Jan
Jan le 18 Août 2012
There is no "red, green, blue" in your code. Better post code, which has a strong relation to the text of the quetsion.

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 18 Août 2012
Modifié(e) : Jan le 18 Août 2012
At first omit the nervous "clc; clf; clear all; close all;". It is a waste of time even and especially when posting example code in the forum. Is there any reason to kill all my work, when I try to run your code?
I use a separate function for a better readability:
figure;
ImageH = imshow('attachment.jpg');
AxesH = ancestor(ImageH, 'axes');
str = {'red', 'green', 'blue'};
hL = uicontrol('style','popup','string',str,'position', [20 20 200 30],...
'callback', {@myCallback, AxesH});
function myCallback(src, evt, AxesH)
TitleH = get(AxesH, 'Title');
oldTitle = get(TitleH, 'String');
StrList = get(src, 'String');
selected = StrList{get(src, 'Value')};
if isempty(oldTitle)
set(TitleH, 'String', sprintf('The objects are: %s', selected));
else
set(TitleH, 'String', [oldTitle, ', ', selected]);
end

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by