【问题标题】:MATLAB GUI history boxMATLAB GUI 历史记录框
【发布时间】:2015-05-20 12:01:52
【问题描述】:

我想创建一个编辑框来接收我将在算法中使用的命令,并将这些命令保存到另一个编辑/列表框以供再次使用。有人可以帮忙吗?我正在为我的 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)

【问题讨论】:

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


    【解决方案1】:

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

    解释在 cmets 中:

    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
    

    它的样子:

    希望有帮助!

    【讨论】:

    • 您好,感谢您的解决方案!这对于非 GUIDE GUI 非常有用,但是当我将它插入我的 GUIDE GUI 时,hCounter 似乎没有得到更新。这是我的添加到历史按钮回调中的内容: function pushbutton5_Callback(hObject, eventdata, handles) hCounter = 0; CurrentCommand = cellstr(get(handles.edit3,'String')); CurrentHistory = cellstr(get(handles.listbox4,'String')); NewHistory = vertcat(CurrentHistory,CurrentCommand);如果 hCounter ==0 NewHistory(1) = [];结束集(handles.listbox4,'String',NewHistory)
    • 哦,然后将其存储在handles 结构中,这应该可以工作。例如,将其命名为 handles.Counter,例如在您的 GUI 的 Opening_Fcn 中。
    • 我将handles.Counter 添加到Opening_Fcn。然后我在 Button 回调中将其称为handles.Counter = 0 并继续在回调中将其引用为handles.Counter,它似乎仍然没有得到更新。
    • 嗯,你在回调结束时使用guidata(hObject,handles)吗?
    • 是的:% 更新句柄结构 guidata(hObject, handles);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2016-07-04
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多