2

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.

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. Commented Mar 31 at 16:31
  • Yes, you are indeed right, it is Octave. Thanks for your comment ! Commented Apr 1 at 6:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, you are right, it's obvious now you say it. Is there a way to create them outide the loop ?
It might be easier to manipulate the data outside the loops if you were to store each variable as a matrix, i.e. form.Cr(j,m,k) instead of form(j,m,k).Cr and form.c(j,m,k,i) instead of form(j,m,k).c(i), because you can then do operations on slices of data, although it's not clear to me why you want to duplicate the data in c splitting it into c_top and c_bot, maybe just because the matrix operations aren't easy in the current structure?
The purpose of the code is to create several geometries which I store as a structure. Then, each geometry corresponds to form(j,m,k). I declare variable so : form = struct; Cr = [10 15 20]; TR = [0 0.3 0.5 1]; H = 45; pH = [0 0.3 0.5 0.7]; I split then the data c to solve matrix equation further in the code.

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.