Search code examples
arraysmatlabstructcell

Weird behaviour using IF statements and structs in Matlab?


The output is always two values when it is supposed to be only 1 ..

s is a struct where

1x1024 struct array with fields:
    ID
    s1
    s2
    s3
    s4
    PB1
    PB2
    PB3
    PB4
    eG
    next

I have the following loop :

for t=1:length(s)

if s(t).eG==0


 if s(t).s1==1

    if s(t).PB1==0
        slackp(t)=0; 
    elseif s(t).PB1==1
        slackp(t)=350;
    elseif s(t).PB1==2
        slackp(t)=600;
    elseif s(t).PB1==3
        slackp(t)=750;
    end
end

 if s(t).s2==1

    if s(t).PB2==0
        slackp2(t)=0; 
    elseif s(t).PB2==1
        slackp2(t)=500;
    elseif s(t).PB2==2
        slackp2(t)=620;
    elseif s(t).PB2==3
        slackp2(t)=785;
    end

  end
 end
end

However I notice that at the following statement at t=2

        elseif s(t).PB1==1
        slackp(t)=350;

It always prints

 slackp(1)=[0 350] 

The error carries forward and multiple other entries have 0 alongside with them !! Why is this happening ? I am just trying to store 350, I don't want a 0 there !

I tried debugging the problem, and realised that whenever s1 is not =1, it will print a 0. It shouldn't. If s1 is not 1 then just skip the IF statement. Same goes for s2.


Solution

  • To get around this problem you can use a different variable to index slackp than to index s. For example:

    clear all
    s(1).s1 = 0;
    s(1).PB1 = 2;
    s(1).PB2 = 2;
    s(1).s2 = 0;
    s(2).s1 = 1;
    s(2).s2 = 1;
    s(2).PB1 = 1;
    s(2).PB2 = 3;
    s(3).s1 = 1;
    s(3).PB1 = 2;
    s(3).s2 = 1;
    s(3).PB2 = 2;
    
    index1 = 1;
    index2 = 1;
    for t=1:length(s)
    if s(t).s1==1
        if s(t).PB1==0
            slackp(inde1x)=0; 
             index1 = index1 + 1;
        elseif s(t).PB1==1
            slackp(index1)=350;
             index1 = index1 + 1;
        elseif s(t).PB1==2
            slackp(index1)=600;
             index1 = index1 + 1;
        elseif s(t).PB1==3
            slackp(index1)=750;
             index1 = index1 + 1;
        end
    
    end 
    
    
    if s(t).s2==1
    
        if s(t).PB2==0
            slackp2(index2)=0; 
            index2 = index2 + 1;
        elseif s(t).PB2==1
            slackp2(index2)=500;
            index2 = index2 + 1;
        elseif s(t).PB2==2
            slackp2(index2)=620;
            index2 = index2 + 1;
        elseif s(t).PB2==3
            slackp2(index2)=785;
            index2 = index2 + 1;
        end
    
        end
    
     end
    

    Will give you:

    slackp =
    

    350 600

    slackp2 =
    

    785 620

    Alternately, you can use end + 1 to index your output array, like this:

    slackp = [];
    for t=1:length(s)
        if s(t).s1==1
            if s(t).PB1==0
                slackp(end + 1)=0; 
            elseif s(t).PB1==1
                slackp(end + 1)=350;
            elseif s(t).PB1==2
                slackp(end + 1)=600;
            elseif s(t).PB1==3
                slackp(end + 1)=750;
            end
        end 
     end