-1

I'd like to create an edit box that takes in commands I'll be using in my algorithm and save those commands to another edit/listbox to be used again. Can anybody help? I'm using GUIDE for my GUI. Thanks!

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GeneralPlotter_OpeningFcn, ...
                   'gui_OutputFcn',  @GeneralPlotter_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GeneralPlotter is made visible.
function GeneralPlotter_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to GeneralPlotter (see VARARGIN)

% Choose default command line output for GeneralPlotter
handles.output = hObject;

if isempty(varargin)
    handles.gp = GenericPlotter();
else
    handles.gp = [];
end
set(handles.edit3,'string','')
handles.Counter = 0;

% Update handles structure

guidata(hObject, handles);

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%// Counter to know how many functions you added
handles.Counter = 0;

%// Pushbutton's callback. Get the string in the edit box and append it
%// to the listbox content. Delete the 1st entry since its intially empty

CurrentCommand = cellstr(get(handles.edit3,'String'));

CurrentHistory = cellstr(get(handles.listbox4,'String'));

NewHistory = vertcat(CurrentHistory,CurrentCommand);

%// Remove 1st empty entry on 1st press of the button
if handles.Counter == 0
    NewHistory(1) = [];
end
set(handles.listbox4,'String',NewHistory)
handles.Counter = handles.Counter + 1;
guidata(hObject,handles)
greg
  • 149
  • 1
  • 10

1 Answers1

1

Your question is not very clear but here is some code to get you going. Basically the user enters commands in the edit box and upon pressing a button, the content of the box is appended to the listbox.

Explanations are in comments:

function HistoryGUI
clear
clc

hfigure = figure('Position',[200 200 300 300]);

hText1 = uicontrol('Style','Text','Position',[20 220 100 20],'String','Enter command');
hEdit1 = uicontrol('Style','edit','Position',[20 200 100 20],'String','');

hButton = uicontrol('Style','push','Position',[20 160 100 20],'String','Add to History','Callback',@(s,e) ButtonCallback);

hList = uicontrol('Style','list','Position',[150 150 100 80],'String','');

%// Counter to know how many functions you added
hCounter = 0;

%// Pushbutton's callback. Get the string in the edit box and append it
%// to the listbox content. Delete the 1st entry since its intially empty
    function ButtonCallback

        CurrentCommand = cellstr(get(hEdit1,'String'));

        CurrentHistory = cellstr(get(hList,'String'));

        NewHistory = vertcat(CurrentHistory,CurrentCommand);

        %// Remove 1st empty entry on 1st press of the button
        if hCounter ==0
            NewHistory(1) = [];
        end

        set(hList,'String',NewHistory)

        hCounter= hCounter + 1;
    end
end

What it looks like:

enter image description here

Hope that helps!

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
  • Hi, thanks for your solution! This works great for a non-GUIDE GUI, however when I plug it into my GUIDE GUI the hCounter doesn't seem to get updated. Here is what I have in my Add to history button callback: function pushbutton5_Callback(hObject, eventdata, handles) hCounter = 0; CurrentCommand = cellstr(get(handles.edit3,'String')); CurrentHistory = cellstr(get(handles.listbox4,'String')); NewHistory = vertcat(CurrentHistory,CurrentCommand); if hCounter ==0 NewHistory(1) = []; end set(handles.listbox4,'String',NewHistory) – greg Mar 18 '15 at 14:41
  • Oh then store it in the `handles` structure and that should work. i.e. name it `handles.Counter` for example in the `Opening_Fcn` of your GUI. – Benoit_11 Mar 18 '15 at 14:42
  • I added handles.Counter to the Opening_Fcn. Then I call it in the Button callback as handles.Counter = 0 and continue to reference it as handles.Counter in the callback and it still doesn't seem to get updated. – greg Mar 18 '15 at 14:49
  • Mhh and do you use `guidata(hObject,handles)` at the end of the callback? – Benoit_11 Mar 18 '15 at 14:50
  • yep: % Update handles structure guidata(hObject, handles); – greg Mar 18 '15 at 14:52
  • I actually just added guidata(hObject, handles) to the end of the button callback and it still doesn't work. – greg Mar 18 '15 at 14:56
  • Ok and is it also at the end of the opening function? That's weird – Benoit_11 Mar 18 '15 at 14:56
  • ok can you edit your question and put in your code? I don't understand why this doesn't work – Benoit_11 Mar 18 '15 at 15:03
  • Got it! Remove that line in the pushbutton's callback: `%// Counter to know how many functions you added handles.Counter = 0;` The counter is already initialized in the Opening function so this puts it to zero every time you press the button. – Benoit_11 Mar 18 '15 at 15:44
  • Very nice! Appreciate all your time and effort! How do I mark this answered? – greg Mar 18 '15 at 15:49
  • Great you're welcome! You can check the green tick mark beside the top of my answer. Thanks :) – Benoit_11 Mar 18 '15 at 15:53