【问题标题】:Changing displayed string in MATLAB GUI edit box by changing value in another edit box通过更改另一个编辑框中的值来更改 MATLAB GUI 编辑框中显示的字符串
【发布时间】:2015-04-11 04:47:10
【问题描述】:

现在,我正在开发一个基于用户输入数据绘制图形的 GUI。用户使用编辑框输入数字,然后从那里生成图。

我有两个值,周期和频率,我知道它们在数学上是相互依赖的(p=1/f,反之亦然)。我相信我已经做到了,当我编辑频率时,会计算一个新的周期值,但我还想做的是每当用户输入新的频率值时,在周期编辑框中显示周期的新值.为了说明,这里是我现在的频率编辑框的代码:

%period (ns)
function edit11_Callback(hObject, eventdata, handles)
num = str2double(get(hObject,'string'));
%global edit10;%brings frequency to edit11 function
%global edit11;%globalize period
handles.edit11 = num;
handles.edit10 = 1/num;%frequency = 1/period
set(handles.edit10,'String',num2str(1/num));%displays new frequency
guidata(hObject,handles)

注意:edit10 是句号的编辑框。我希望那个代码与我的频率相反。另外,我注释掉了“全局”行,因为我似乎不需要它们来做我想做的事情,但我确实尝试过。

"handles.edit10 = 1/num;"更新期间的实际值。据我所知,最后一行 (guidata(...)) 将全局保存handles.edit10 和handles.edit11 的新值,但如果有错误请纠正我。

不起作用的是行:“set(handles.edit10,'String',num2str(1/num));”,它应该在句点编辑框中显示句点的新值(edit10 )。

我得到的错误信息如下:

Error using handle.handle/set
Invalid or deleted object.

Error in Liposome_GUI>edit11_Callback (line 367)
set(handles.edit10,'String',num2str(1/num));%displays new frequency

Error in gui_mainfcn (line 95)
    feval(varargin{:});

Error in Liposome_GUI (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)Liposome_GUI('edit11_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

错误消息使函数edit11 似乎无法访问存储在handles.edit10 中的值,但如果我将这两个“全局”行放在edit10 和edit11 函数下,似乎没有任何改变。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: matlab user-interface editbox


    【解决方案1】:

    如果您删除了 edit10 句柄的覆盖,会发生什么情况? 你也不需要覆盖edit11...也不需要在这里保存句柄

    edit11有回调时,以下将起作用

    %period (ns)
    function edit11_Callback(hObject, eventdata, handles)
      num = str2double(get(handles.edit11,'string'));
      set(handles.edit10,'String',num2str(1/num)); %displays new frequency
    end
    

    【讨论】:

    • 嗯,如果我尝试这样做,我会收到消息:非法使用保留关键字“end”。如果我去掉那个“结束”行,我仍然会收到与以前相同的错误消息。
    • 如果有帮助,这里是“edit11”的完整代码:嗯,如果我尝试这样做,我会收到消息:非法使用保留关键字“end”。如果我取出那个“结束”行,我仍然会收到与以前相同的错误消息。这是edit11的完整代码,如果有帮助的话: function edit11_Callback(hObject, eventdata, handles) num = str2double(get(handles.edit11,'string'));设置(handles.edit10,'字符串',num2str(1/num)); function edit11_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');结束
    【解决方案2】:

    我认为生成错误是因为您在第 7 行的 edit11_Callback 中使用此命令覆盖了分配给 uicontrol edit10 的值:

    handles.edit10 = 1/num;%frequency = 1/period
    

    然后 MATLAB 认为实际的编辑框句柄已被删除,因为与之关联的每个数据都已被变量 num 覆盖。

    否则您的代码应该可以工作。我制作了一个简单的 GUI,以防你想玩弄编辑框。它所做的只是根据周期/频率更新编辑框和虚拟数据图。一切都被评论了,所以应该很容易理解。如果有不清楚的地方请告诉我。

    function GUI_EditBox
    clc
    clear
    close all
    
    %// Create GUI components
    hFigure = figure('Position',[100 100 500 500],'Units','Pixels');
    
    handles.axes1 = axes('Units','Pixels','Position',[60,90,400,300]);
    
    handles.TextPeriod = uicontrol('Style','Text','Position',[20 470 60 20],'String','Period');
    handles.TestFrequ = uicontrol('Style','Text','Position',[20 440 60 20],'String','Frequency');
    
    handles.EditPeriod = uicontrol('Style','Edit','Position',[100 470 60 20],'String','2*pi','Value',1,'Callback',@(s,e) EditPeriodCallback);
    handles.EditFrequ = uicontrol('Style','Edit','Position',[100 440 60 20],'String','pi/2','Callback',@(s,e) EditFrequCallback);
    
    %// Define initial period and frequency
    handles.Period = 2*pi;
    handles.Frequency = 1/handles.Period;
    
    %// Define x data and function to plot
    handles.x = 0:pi/10:2*pi;
    
    handles.y = rand(1)*sin(handles.Period.*handles.x);
    
    plot(handles.x,handles.y,'Parent',handles.axes1)
    
    guidata(hFigure,handles); %// Save handles structure of GUI.
    
    %// The callbacks for both edit boxes are quite similar. Basically get the value entered by the user and update the other as well as the plot.
        function EditPeriodCallback(~,~)
    
            handles = guidata(hFigure);
    
            handles.Period = str2double(get(handles.EditPeriod,'String'));
            handles.Frequency = 1/handles.Period;
    
            set(handles.EditFrequ,'String',num2str(1/handles.Period));
    
            handles.y = 2*rand(1)*sin(handles.Period.*handles.x);
            plot(handles.x,handles.y,'Parent',handles.axes1)
    
    
            guidata(hFigure,handles);
        end
    
        function EditFrequCallback(~,~)
    
            handles = guidata(hFigure);
    
            handles.Frequency = str2double(get(handles.EditFrequ,'String'));
            handles.Period = 1/handles.Frequency;
    
            set(handles.EditPeriod,'String',num2str(1/handles.Frequency));
    
            handles.y = 2*rand(1)*sin(handles.Period.*handles.x);
            plot(handles.x,handles.y,'Parent',handles.axes1)
    
    
            guidata(hFigure,handles);
        end
    
    end 
    

    GUI 截图示例:

    请注意,添加一个框可能会很有用,用户可以在其中提供 pi 的倍数或类似的东西而不是整数。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多