MATLAB GUI历史记录框

时间:2015-03-17 21:01:24

标签: matlab user-interface textbox listbox matlab-guide

我想创建一个编辑框,其中包含我将在我的算法中使用的命令,并将这些命令保存到另一个编辑/列表框中以便再次使用。有人可以帮忙吗?我使用GUIDE作为我的GUI。谢谢!

% 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)

1 个答案:

答案 0 :(得分:1)

你的问题不是很清楚,但这里有一些代码可以帮助你。基本上用户在编辑框中输入命令,按下按钮后,框的内容将附加到列表框中。

解释在评论中:

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

它看起来像什么:

enter image description here

希望有所帮助!