2016-12-06 416 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語句中使用[]時看到的錯誤。

我猜測,當你以前運行代碼時,的值已經設置在根對象上,並且一切正常。當您稍後重新執行代碼時,該值無論出於何種原因都不會設置在根對象上。這個值是通過其他必須首先運行的其他代碼在根對象上設置的,或者代碼中只有在第二次運行時沒有滿足的某些條件下設置該值。