1

My error is

Field assignment to a non-structure array object.

Error in showTF/cback (line 481)
                tmp.others = get(gcbo,'UserData');

Error while evaluating UIControl Callback

I am trying to convert a character vector callback to a function handle. I try to use a cell array to accept additional input for each condition of the cases. I think the major issue here is the syntax and some knowledge of data sharing between the components.

here is the old code:

cback = ['switch tmp.m, '...
            'case 1;'...
                'tmp.others = get(gcbo,''UserData'');'...
                'tmp.n1 = str2num(get(gcbo,''String''));'...
                'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
                'tmp.J = str2num(get(tmp.others(2),''String''));'...
                'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
            'case 2;'...
                'tmp.others = get(gcbo,''UserData'');'...
                'tmp.n2 = str2num(get(gcbo,''String''));'...
                'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
                'tmp.J = str2num(get(tmp.others(2),''String''));'...
                'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
            'case 3;'...
                'tmp.others = get(gcbo,''UserData'');'...
                'tmp.J = str2num(get(gcbo,''String''));'...
                'tmp.n2 = str2num(get(tmp.others(1),''String''));'...
                'tmp.n1 = str2num(get(tmp.others(2),''String''));'...
                'tmp.Jm = str2num(get(tmp.others(3),''String''));'...
            'case 4;'...
                'tmp.others = get(gcbo,''UserData'');'...
                'tmp.Jm = str2num(get(gcbo,''String''));'...
                'tmp.n1 = str2num(get(tmp.others(1),''String''));'...
                'tmp.n2 = str2num(get(tmp.others(2),''String''));'...
                'tmp.J = str2num(get(tmp.others(3),''String''));'...
        'end;'...
            'tmp.str = [''$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m='' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) ''kg \cdot m^2$'']; '...
            'set(result ,''String'',tmp.str); set(button,''Enable'',''on'',''UserData'',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp'];    

my uicontrol callbacks for each condition

 edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',['tmp.m=1; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
            edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',['tmp.m=2; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
            edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',['tmp.m=3; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);
            edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',['tmp.m=4; ' cback],'BackgroundColor',[1 1 1],'ToolTipString',tt);

Trying to write a nested function:

function cback(~,~,tmp)
        switch tmp
            case 1
                tmp.others = get(gcbo,'UserData');
                tmp.n1 = str2num(get(gcbo,'String'));
                tmp.n2 = str2num(get(tmp.others(1),'String'));
                tmp.J = str2num(get(tmp.others(2),'String'));
                tmp.Jm = str2num(get(tmp.others(3),'String'));
            case 2
                tmp.others = get(gcbo,'UserData');
                tmp.n2 = str2num(get(gcbo,'String'));
                tmp.n1 = str2num(get(tmp.others(1),'String'));
                tmp.J = str2num(get(tmp.others(2),'String'));
                tmp.Jm = str2num(get(tmp.others(3),'String'));
            case 3
                tmp.others = get(gcbo,'UserData');
                tmp.J = str2num(get(gcbo,'String'));
                tmp.n2 = str2num(get(tmp.others(1),'String'));
                tmp.n1 = str2num(get(tmp.others(2),'String'));
                tmp.Jm = str2num(get(tmp.others(3),'String'));
            case 4
                tmp.others = get(gcbo,'UserData');
                tmp.Jm = str2num(get(gcbo,'String'));
                tmp.n1 = str2num(get(tmp.others(1),'String'));
                tmp.n2 = str2num(get(tmp.others(2),'String'));
                tmp.J = str2num(get(tmp.others(3),'String'));
        end
            tmp.str = ['$J=\frac{N_1^2}{N_2^2}\cdot J_{load}+J_m=' num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm) 'kg \cdot m^2$'];
            set(result ,'String',tmp.str);set(button,'Enable','on','UserData',num2str((tmp.J*tmp.n1^2)/(tmp.n2^2)+tmp.Jm)); clear tmp;    

   end

my uicontrol callbacks for each condition

edit(1) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 80 40 20],'String','1','Style','edit','Callback',{@cback,1},'BackgroundColor',[1 1 1],'ToolTipString',tt);
    edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 60 40 20],'String','1','Style','edit','Callback',{@cback,2},'BackgroundColor',[1 1 1],'ToolTipString',tt);
    edit(3) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 40 40 20],'String','0','Style','edit','Callback',{@cback,3},'BackgroundColor',[1 1 1],'ToolTipString',tt);
    edit(4) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[30 20 40 20],'String','0.0019','Style','edit','Callback',{@cback,4},'BackgroundColor',[1 1 1],'ToolTipString',tt);

1 Answer 1

1

Within your function cback, you already have the variable tmp defined as a scalar with a value of 1 through 4. You then immediately try to add a field others to it with tmp.others = .... This explains your error:

Field assignment to a non-structure array object.

If tmp is already initialized to be a scalar double variable, you can't just assign structure fields to it.

There's no real reason for you to do this anyway, since you can just make another variable. Just change your top lines to this:

function cback(~,~,editIndex)
    switch editIndex
        case 1
            ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @gnovice!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.