GUI adding elements in listbox

3 vues (au cours des 30 derniers jours)
Kris zenitis
Kris zenitis le 17 Mai 2011
Commenté : Azzi Abdelmalek le 19 Fév 2014
Hello there. I m trying to create a gui. I firstly design it via guide command. I ve designed a list box and I was wandering how it is possible to add new elements in this listbox.

Réponse acceptée

Matt Fig
Matt Fig le 17 Mai 2011
This is from my collection of GUIs for learning on the FEX.
function [] = GUI_2()
% Demonstrate how to add a new entry to a uicontrol string.
% Creates a listbox with some strings in it, an editbox and a pushbutton.
% User types some text into the editbox, then pushes the pushbutton. The
% user's text will be added to the top of the listbox.
%
% Suggested exercise: Modify the code so that hitting return after a
% string is typed performs the same task as pushing the pushbutton.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[500 500 200 300],...
'menubar','none',...
'name','GUI_2',...
'numbertitle','off',...
'resize','off');
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[10 110 180 180],...
'min',0,'max',2,...
'fontsize',14,...
'string',{'one';'two';'three';'four'});
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[10 60 180 30],...
'fontsize',14,...
'string','New String');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Add String',...
'callback',{@ed_call,S});
function [] = ed_call(varargin)
% Callback for pushbutton, adds new string from edit box.
S = varargin{3}; % Get the structure.
oldstr = get(S.ls,'string'); % The string as it is now.
addstr = {get(S.ed,'string')}; % The string to add to the stack.
% The order of the args to cat puts the new string either on top or bottom.
set(S.ls,'str',{addstr{:},oldstr{:}}); % Put the new string on top -OR-
% set(S.ls,'str',{oldstr{:},addstr{:}}); % Put the new string on bottom.
  3 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 19 Fév 2014
[Kris commented]
Actually im trying this to parse a string from edit box to button so as to open a specific image.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h=findobj('Tag','edit1');
input = get(h,'String');
input1 = strcat(input{1, 1}, '.jpg');
disp(input1);
I=imread( strcat(input{1, 1}, '.jpg'));
disp returns the real name of the picture but in imread i ve got this problem
Conversion to logical from cell is not possible.
Error in ==> imread at 342 if (strfind(filename, '://'))
Error in ==> untitled>pushbutton1_Callback at 85 I=imread( strcat(input{1, 1}, '.jpg'));
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> untitled at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)untitled('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Azzi Abdelmalek
Azzi Abdelmalek le 19 Fév 2014
[Kris commented]
I got. I had to convert cell inpu to string with function char. Now works. I want to ask sth else. In the same button i ve a command to imshow(it shows me a specific image in the background) a specific image but i want to imshow the image in a new window. What can I do?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by