2016-12-06 429 views
0

我有一个非常奇怪的错误......我得到如下:Matlab的GUI回调错误

SWITCH expression must be a scalar or string constant. 
Error in RL_Bsp>WechselStatus (line 387) 
    switch GewichtungNutzer 
Error in RL_Bsp>togglebutton1_Callback (line 151) 
    WechselStatus(Status, Aktion, ButtonWert); 
Error in gui_mainfcn (line 95) 
     feval(varargin{:}); 
Error in RL_Bsp (line 42) 
    gui_mainfcn(gui_State, varargin{:}); 
Error in 
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)RL_Bsp('togglebutton1_Callback',hObject,eventdata,guidata(hObject)) 
Error while evaluating UIControl Callback 

有趣的是,我什么都没有改变,当我重新启动我的电脑前面,它只是开始发生就在那里习惯在10小时前工作!

的错误似乎是在这里:

function WechselStatus(Status, Aktion, ButtonWert) 
    GewichtungNutzer = getappdata(0,'Wert'); 
    global R_Alg 
    global Ziel 
    switch GewichtungNutzer 
     case {'100'} 
      GewichtungNutzer = 100; 
     case {'200'} 
      GewichtungNutzer = 200; 
     case {'300'} 
      GewichtungNutzer = 300; 
     case {'Ziel mit 500'} 
      GewichtungNutzer = 0; 
     otherwise 
      GewichtungNutzer = -1; 
    end  

    if get(ButtonWert,'value') == 1 
     set(ButtonWert,'Backgroundcolor','0.76, 0.87, 0.78'); 
     if GewichtungNutzer > 0 
      R_Alg(R_Alg(:,Aktion)==0, Aktion) = GewichtungNutzer; 
     else 
      R_Alg(R_Alg(:,Aktion)==0, Aktion) = 500; 
      Ziel = Aktion; 
     end 
    elseif get(ButtonWert,'value') == 0 
     set(ButtonWert,'Backgroundcolor','0.11, 0.31, 0.21'); 
     R_Alg(:, Aktion) = -1; 
    end 

这里

function togglebutton1_Callback(hObject, eventdata, handles) 
    Status = 1; 
    Aktion = 1; 
    ButtonWert = hObject; 
    WechselStatus(Status, Aktion, ButtonWert); 

我真是不知道为什么我得到的错误,现在我读很多次的东西做的路径代码无法读取gui?会感谢帮助!

+2

错误消息告诉你问题出在哪里:'GewichtungNutzer'不是标量或字符串。考虑到你如何获得它,'getappdata(0,'Wert')'要么返回一个空数组或向量。 – excaza

+0

但不是很奇怪,我改变了绝对没有什么比10小时前,我现在得到它,而它以前工作完美罚款? – spr1te

+0

如果你实际上没有改变任何东西,也许。我非常怀疑这种情况。 – excaza

回答

2

这条线:

GewichtungNutzer = getappdata(0,'Wert'); 

使用函数getappdata来检索已存储在图形的值'Wert'对象0。图形句柄0总是指root object。为了使该行按预期运行,必须首先使用setappdata将该值添加到根对象。如果尚未初始化,它将返回[],这会给您在尝试在switch语句中使用[]时看到的错误。

我猜测,当你以前运行代码时,的值已经设置在根对象上,并且一切正常。当您稍后重新执行代码时,该值无论出于何种原因都不会设置在根对象上。这个值是通过其他必须首先运行的其他代码在根对象上设置的,或者代码中只有在第二次运行时没有满足的某些条件下设置该值。