【问题标题】:MATLAB GUI - Making Checkbox GlobalMATLAB GUI - 使复选框全局
【发布时间】:2015-01-18 02:19:45
【问题描述】:

我有一个MATLAB GUI,它为用户提供了勾选选项。每个刻度都有不同的数学意义。用户勾选某些内容后,计算将在他们按下“计算”后完成。

我在“计算”按钮下的主要公式是:

effective_weight = weight + pilotsw + fo_weight  %pilots weight & first officer's weight

pilots_weight & fo_weight 有不同的复选框,它们的代码如下:

function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
% --- Executes on button press in checkbox3.

if (get(hObject,'Value') == get(hObject,'Max'))

pilotw = -100

else

 pilotw = 0

end

全球飞行员(和副驾驶的复选框相同)

并且 Pilotw 是计算函数的全局变量。

目标是改变有效重量,并在飞行员在场(或不在场)时进行相应的计算。

【问题讨论】:

  • 您的问题/疑问究竟是什么?

标签: matlab user-interface checkbox global


【解决方案1】:

为了访问全局变量pilotw,你需要在你的函数中这样声明它:

function checkbox2_Callback(hObject, eventdata, handles)

        %'Declare global variable'
        global pilotw

        %'Set global variable'
        if get(hObject,'Value') == get(hObject,'Max')
                pilotw = -100

        else
                pilotw = 0
        end;
end

【讨论】:

  • 是的,这有帮助。并且 Pilotw 应该在代码的开头进行初始化。 (即 Pilotw = 0)
【解决方案2】:

您需要在要使用此变量的每个函数中声明全局 Pilotw。

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 2013-05-06
    • 2014-05-09
    • 2010-10-21
    • 2014-10-20
    • 2014-10-27
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多