Add Array in a Cell in a For Loop

9 vues (au cours des 30 derniers jours)
Fabio Taccaliti
Fabio Taccaliti le 26 Avr 2022
Commenté : Fabio Taccaliti le 26 Avr 2022
Hello,
I would like to add 6 array in a cell (in a new row for each for loop iteration), how can I do it?
The 6 arrays are x_wing, y_wing, z_wing and also x_winglet, y_winglet, z_winglet and the cell is Coordinates that I created outside the loop with the correct dimensions, but when I'm doing Coordinates{i} = {x_wing, y_wing, z_wing, x_wingtip, y_wingtip, z_wingtip}; it filling just the first column of the cell.
This is what I did right now.
Thanks
Coordinates = cell(length(Coo2(:,7)),6);
for i = 1:length(Coo2(:,7))
x = (5:30:95)/100;
x = [x x];
z_wing = (366.2966:15:700-193.7-5)/100;
z_wingtip = (545:30:695)/100;
y = 5*0.18*(0.2969*sqrt(x(1:4))-0.126*x(1:4)-0.3516*x(1:4).^2+0.2843*x(1:4).^3-0.1036*x(1:4).^4);
y = [y -y];
% Angle of Attack
x1 = [0.5+(x(1)-0.5)*cosd(5)-y(1)*sind(5), 0.5+(x(2)-0.5)*cosd(5)-y(2)*sind(5), 0.5+(x(3)-0.5)*cosd(5)-y(3)*sind(5), 0.5+(x(4)-0.5)*cosd(5)-y(4)*sind(5), 0.5+(x(5)-0.5)*cosd(5)-y(5)*sind(5), 0.5+(x(6)-0.5)*cosd(5)-y(6)*sind(5), 0.5+(x(7)-0.5)*cosd(5)-y(7)*sind(5), 0.5+(x(8)-0.5)*cosd(5)-y(8)*sind(5)];
y1 = [(x(1)-0.5)*sind(5)+y(1)*cosd(5), (x(2)-0.5)*sind(5)+y(2)*cosd(5), (x(3)-0.5)*sind(5)+y(3)*cosd(5), (x(4)-0.5)*sind(5)+y(4)*cosd(5), (x(5)-0.5)*sind(5)+y(5)*cosd(5), (x(6)-0.5)*sind(5)+y(6)*cosd(5), (x(7)-0.5)*sind(5)+y(7)*cosd(5), (x(8)-0.5)*sind(5)+y(8)*cosd(5)];
x_wing = x1;
y_wing = y1;
wingtip_angle = Coo2(i,7);
y_1 = -(z_wingtip(1)-5.062) * sind(wingtip_angle);
y_2 = -(z_wingtip(2)-5.062) * sind(wingtip_angle);
y_3 = -(z_wingtip(3)-5.062) * sind(wingtip_angle);
y_4 = -(z_wingtip(4)-5.062) * sind(wingtip_angle);
y_5 = -(z_wingtip(5)-5.062) * sind(wingtip_angle);
y_6 = -(z_wingtip(6)-5.062) * sind(wingtip_angle);
x_wingtip = repmat(x_wing, 6, 1);
y_wingtip = [y_1; y_2; y_3; y_4; y_5; y_6];
y_wingtip = repmat(y_wingtip, 1, 8);
y_wingtip = [y_wingtip(:,1)+y_wing(1)*cosd(wingtip_angle), y_wingtip(:,2)+y_wing(2)*cosd(wingtip_angle), y_wingtip(:,3)+y_wing(3)*cosd(wingtip_angle), y_wingtip(:,4)+y_wing(4)*cosd(wingtip_angle), y_wingtip(:,5)+y_wing(5)*cosd(wingtip_angle), y_wingtip(:,6)+y_wing(6)*cosd(wingtip_angle), y_wingtip(:,7)+y_wing(7)*cosd(wingtip_angle), y_wingtip(:,8)+y_wing(8)*cosd(wingtip_angle)];
z_wingtip = 5.062 + (z_wingtip-5.062) .* cosd(wingtip_angle);
z_wingtip = repmat(z_wingtip, length(x_wing), 1)';
z_wingtip = [z_wingtip(:,1)+y_wing(1)*sind(wingtip_angle), z_wingtip(:,2)+y_wing(2)*sind(wingtip_angle), z_wingtip(:,3)+y_wing(3)*sind(wingtip_angle), z_wingtip(:,4)+y_wing(4)*sind(wingtip_angle), z_wingtip(:,5)+y_wing(5)*sind(wingtip_angle), z_wingtip(:,6)+y_wing(6)*sind(wingtip_angle), z_wingtip(:,7)+y_wing(7)*sind(wingtip_angle), z_wingtip(:,8)+y_wing(8)*sind(wingtip_angle)];
x_wing = repmat(x_wing, 8, 1);
y_wing = repmat(y_wing, 8, 1);
z_wing = repmat(z_wing(3:end), length(x_wing), 1)';
Coordinates{i} = {x_wing, y_wing, z_wing, x_wingtip, y_wingtip, z_wingtip};
clearvars x_wing y_wing z_wing x_wingtip y_wingtip z_wingtip;
end

Réponse acceptée

Mohammad Sami
Mohammad Sami le 26 Avr 2022
Modifié(e) : Mohammad Sami le 26 Avr 2022
Your current code is creating a nested cell array. You can change it as follows.
% for testing %
i = 1;
x_wing = 1;
y_wing = 2;
z_wing = 3;
x_wingtip = 4;
y_wingtip = 5;
z_wingtip = 6;
% change as follows
Coordinates(i,:) = {x_wing, y_wing, z_wing, x_wingtip, y_wingtip, z_wingtip};
% for testing %
i = 2;
Coordinates(i,:) = {x_wing, y_wing, z_wing, x_wingtip, y_wingtip, z_wingtip};
Coordinates
Coordinates = 2×6 cell array
{[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]}
  1 commentaire
Fabio Taccaliti
Fabio Taccaliti le 26 Avr 2022
Thanks Mohammad for the answer! Now it works :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by