【问题标题】:Weird behaviour using IF statements and structs in Matlab?在 Matlab 中使用 IF 语句和结构的奇怪行为?
【发布时间】:2013-04-07 21:15:20
【问题描述】:

当它应该只有 1 时,输出总是两个值..

s 是一个结构体,其中

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

我有以下循环:

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

但是我注意到,在 t=2 的以下语句中

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

它总是打印出来

 slackp(1)=[0 350] 

错误结转,其他多个条目旁边有 0 !!为什么会这样?我只是想存储 350,我不想要 0 !

我尝试调试问题,并意识到只要 s1 不 =1,它就会打印一个 0。它不应该。如果 s1 不是 1 则跳过 IF 语句。 s2 也是如此。

【问题讨论】:

  • '但是我注意到,在以下 t=2 的声明中'。这是因为 slackp(t)=350; 正在做 slackp(2)=350; 这意味着它将第二个元素设置为 350。
  • 如果 slackp 为空,则向 slackp(2) 添加一个元素,您将得到一个 2 元素数组,其中第一个元素为 0。这就是这里发生的情况吗?

标签: arrays matlab struct cell


【解决方案1】:

要解决这个问题,您可以使用不同的变量来索引 slackp,而不是索引 s。例如:

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

会给你:

slackp =

350 600

slackp2 =

785 620

或者,您可以使用 end + 1 来索引您的输出数组,如下所示:

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

【讨论】:

  • 这对我不起作用,我不知道为什么。我更新了代码,进一步调试它并意识到只有在有额外的 IF 语句时才会发生这种情况。
  • extra if state 是什么意思?您是否为 slackp2 使用单独的索引?
  • 不,我对整个 for 循环使用相同的索引值。我在更新的问题中添加了一个额外的 IF 语句。它是if s(t).eG==0 .. 显然,如果我不使用它,值将被正确打印和存储,否则,额外的零开始出现。
  • 或者 IF 语句没有做任何事情,我不确定,但结果仍然打印有额外的零。
  • 好的,我误解了你的问题。如果您从不想要额外的零,则仅在向 slackp 添加内容时才需要增加索引。我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多