MATLAB GUI 历史框

MATLAB GUI history box

我想创建一个编辑框来接收我将在我的算法中使用的命令,并将这些命令保存到另一个 edit/listbox 以供再次使用。有人可以帮忙吗?我正在为我的 GUI 使用 GUIDE。谢谢!

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

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

解释在评论中:

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

外观:

希望对您有所帮助!