最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

octave - Error: Invalid dot name structure assignment because the structure array is empty - Stack Overflow

programmeradmin1浏览0评论

I'm having this error but I don't understand why. I'm creating a structure of size 3x4x4 with 6 fields within loops. Then, I would like to create two more fields for the whole structure. How can I do it?

for i = 1:N
  for j=1:length(Cr)
    for m=1:length(TR)
      for k=1:length(pH)
        form(j,m,k).name = ["c" num2str(Cr(j)) "TR" num2str(TR(m)) "ph" num2str(pH(k))];
        form(j,m,k).Cr = Cr(j);
        form(j,m,k).Ct = Cr(j)*TR(m); % corde tip
        form(j,m,k).S = (form(j,m,k).Cr+form(j,m,k).Ct)*H/2;
        form(j,m,k).AR = H^2./form(j,m,k).S;
          if (pH(k) == 0)
            form(j,m,k).c(i) = form(j,m,k).Ct*cos(phi(i))+form(j,m,k).Cr*(1-cos(phi(i)));
          elseif (yh(i) < H*pH(k))
            form(j,m,k).c(i) = form(j,m,k).Cr;
          else
            form(j,m,k).c(i) = form(j,m,k).Cr+(form(j,m,k).Ct-form(j,m,k).Cr)/(H-pH(k)*H)*(yh(i)-pH(k)*H);
          endif
       end
    end
  end
end

form.c_top = form.c(N/2+1:end);
form.c_bot = form.c(1:N/2);

I would like that form(x,y,z).c_top = form(x,y,z).c(N/2+1:end), and this for the whole structure.

I'm having this error but I don't understand why. I'm creating a structure of size 3x4x4 with 6 fields within loops. Then, I would like to create two more fields for the whole structure. How can I do it?

for i = 1:N
  for j=1:length(Cr)
    for m=1:length(TR)
      for k=1:length(pH)
        form(j,m,k).name = ["c" num2str(Cr(j)) "TR" num2str(TR(m)) "ph" num2str(pH(k))];
        form(j,m,k).Cr = Cr(j);
        form(j,m,k).Ct = Cr(j)*TR(m); % corde tip
        form(j,m,k).S = (form(j,m,k).Cr+form(j,m,k).Ct)*H/2;
        form(j,m,k).AR = H^2./form(j,m,k).S;
          if (pH(k) == 0)
            form(j,m,k).c(i) = form(j,m,k).Ct*cos(phi(i))+form(j,m,k).Cr*(1-cos(phi(i)));
          elseif (yh(i) < H*pH(k))
            form(j,m,k).c(i) = form(j,m,k).Cr;
          else
            form(j,m,k).c(i) = form(j,m,k).Cr+(form(j,m,k).Ct-form(j,m,k).Cr)/(H-pH(k)*H)*(yh(i)-pH(k)*H);
          endif
       end
    end
  end
end

form.c_top = form.c(N/2+1:end);
form.c_bot = form.c(1:N/2);

I would like that form(x,y,z).c_top = form(x,y,z).c(N/2+1:end), and this for the whole structure.

Share Improve this question edited 2 days ago kikon 9,0753 gold badges8 silver badges37 bronze badges asked Mar 31 at 14:45 GMICGMIC 211 bronze badge New contributor GMIC is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • For a start, is this Octave code rather than MATLAB code? endif is not valid MATLAB syntax, it would be best if you tagged your question for the specific language you're using to get answers you can definitely use. – Wolfie Commented Mar 31 at 16:31
  • Yes, you are indeed right, it is Octave. Thanks for your comment ! – GMIC Commented Apr 1 at 6:24
Add a comment  | 

1 Answer 1

Reset to default 1

Consider moving the i loop to just wrap the if statement, you're doing loads of overwrites of the elements which are not dependent on i.

Then it also becomes more obvious where to put your assignments for form(x,y,z).c_top, you can just do it after the i loop for each struct c

for j=1:length(Cr)
  for m=1:length(TR)
    for k=1:length(pH)
      form(j,m,k).name = ["c" num2str(Cr(j)) "TR" num2str(TR(m)) "ph" num2str(pH(k))];
      form(j,m,k).Cr = Cr(j);
      form(j,m,k).Ct = Cr(j)*TR(m); % corde tip
      form(j,m,k).S = (form(j,m,k).Cr+form(j,m,k).Ct)*H/2;
      form(j,m,k).AR = H^2./form(j,m,k).S;

      for i = 1:N
        if (pH(k) == 0)
          form(j,m,k).c(i) = form(j,m,k).Ct*cos(phi(i))+form(j,m,k).Cr*(1-cos(phi(i)));
        elseif (yh(i) < H*pH(k))
          form(j,m,k).c(i) = form(j,m,k).Cr;
        else
            form(j,m,k).c(i) = form(j,m,k).Cr+(form(j,m,k).Ct-form(j,m,k).Cr)/(H-pH(k)*H)*(yh(i)-pH(k)*H);
        end
      end

      form(j,m,k).c_top = form(j,m,k).c(N/2+1:end);
      form(j,m,k).c_bot = form(j,m,k).c(1:N/2);
    end
  end
end
发布评论

评论列表(0)

  1. 暂无评论