0

现在,我正在开发一个基于用户输入数据绘制图形的 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 函数下似乎没有任何改变。

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

4

2 回答 2

0

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

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 的倍数或类似的东西而不是整数。

希望有帮助!

于 2015-02-11T13:48:10.670 回答
0

如果删除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
于 2015-02-10T23:32:23.633 回答