简体   繁体   中英

Matlab GUI callback error

i have a really strange error... i get the following:

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

The funny part is that i changed nothing, after i restarted my pc earlier it just started to happen where it used to work 10h ago!

The errors seem to be here:

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

and here

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

i got really no clue why i get the error now i was reading many times its something to do with the path that the code can't read the gui? would appreciate help !!

This line:

GewichtungNutzer = getappdata(0,'Wert');

is using the function getappdata to retrieve the value 'Wert' that has been stored in the graphics object 0 . Graphics handle 0 always refers to the root object . In order for this line to function as intended, that value must be added to the root object first using setappdata . If it hasn't been initialized, it will return [] , which will give you the error you're seeing when you try to use [] in a switch statement.

I'm guessing that, when you had previously run the code, the value of 'Wert' had been set on the root object and everything ran fine. When you reran the code later, this value was, for whatever reason, not set on the root object. Either this value is set on the root object by some other piece of code that you have to run first, or there is a place in your code where the value is only set under certain conditions that were not met the second time you ran it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM