Figures not showing up with for loop
5 次查看(过去 30 天)
显示 更早的评论
Here is the for loop I'm using (I'm putting the whole thing for context), there should be 6 figures total, but I have only been able to get 2:
for i = 3:5 %for reynolds numbers 3e5 through 5e5
for j = 1:16 %angles of attack -6 through 24
temporary_data = data{i,j};
for k = 1:44 %data columns
deltaP = mean(temporary_data(k, 7:16)); % Delta P at specific airfoil sensor
x = ((temporary_data(k, 1))/1000)/chord;
temp = temporary_data(k,6)-32*(5/9)+273.15;
q_transducer = temporary_data(k,5).*47.88;
atmospheric = temporary_data(k,4).*6894.76;
rho = atmospheric/(R.*temp);
velocity = sqrt(2*q_transducer/rho); % Free-stream Velocity
C_p = deltaP/(.5*rho*(velocity^2)); % Finding Coefficient of Pressure
% Assign values to arrays
%velocity_array(i, j, k) = velocity;
CpArray = [CpArray; C_p];
xArray = [xArray; x];
end
C_l = -1/chord * trapz(CpArray * cos(pi()/180 * alpha_range(j)));
ClArray = [ClArray; C_l];
n1 = i-2;
figure(n1)
grid on
hold on
plot(xArray, CpArray, 'm')
set(gca, 'Ydir', 'reverse')
xlabel('x/c')
ylabel('C_p')
titleVar = ['C_p vs. x/c for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
n2=n1+3;
figure(n2)
plot(-6:2:24, ClArray, '-*k')
xlabel('alpha')
ylabel('Cl')
titleVar = ['Cl vs. alpha for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
3 个评论
回答(1 个)
Voss
2024-3-28
Be sure to reset ClArray (and CpArray, xArray and whatever else should not carry over from one iteration of the outer (i) loop to the next) on each i loop iteration.
For example, doing that gives 6 figures:
data = repmat({rand(44,16)},5,16);
chord = 1;
R = 1;
alpha_range = rand(1,16);
for i = 3:5 %for reynolds numbers 3e5 through 5e5
ClArray = [];
CpArray = [];
xArray = [];
for j = 1:16 %angles of attack -6 through 24
temporary_data = data{i,j};
for k = 1:44 %data columns
deltaP = mean(temporary_data(k, 7:16)); % Delta P at specific airfoil sensor
x = ((temporary_data(k, 1))/1000)/chord;
temp = temporary_data(k,6)-32*(5/9)+273.15;
q_transducer = temporary_data(k,5).*47.88;
atmospheric = temporary_data(k,4).*6894.76;
rho = atmospheric/(R.*temp);
velocity = sqrt(2*q_transducer/rho); % Free-stream Velocity
C_p = deltaP/(.5*rho*(velocity^2)); % Finding Coefficient of Pressure
% Assign values to arrays
%velocity_array(i, j, k) = velocity;
CpArray = [CpArray; C_p];
xArray = [xArray; x];
end
C_l = -1/chord * trapz(CpArray * cos(pi()/180 * alpha_range(j)));
ClArray = [ClArray; C_l];
n1 = i-2;
figure(n1)
grid on
hold on
plot(xArray, CpArray, 'm')
set(gca, 'Ydir', 'reverse')
xlabel('x/c')
ylabel('C_p')
titleVar = ['C_p vs. x/c for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
n2=n1+3;
figure(n2)
plot(-6:2:24, ClArray, '-*k')
xlabel('alpha')
ylabel('Cl')
titleVar = ['Cl vs. alpha for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
But initializing them before the loop produces an error when you try to plot ClArray against 16 x values because ClArray no longer has 16 elements the second time through the loop (i=4), and you end up with only 3 figures created.
CpArray = [];
xArray = [];
ClArray = [];
for i = 3:5 %for reynolds numbers 3e5 through 5e5
for j = 1:16 %angles of attack -6 through 24
temporary_data = data{i,j};
for k = 1:44 %data columns
deltaP = mean(temporary_data(k, 7:16)); % Delta P at specific airfoil sensor
x = ((temporary_data(k, 1))/1000)/chord;
temp = temporary_data(k,6)-32*(5/9)+273.15;
q_transducer = temporary_data(k,5).*47.88;
atmospheric = temporary_data(k,4).*6894.76;
rho = atmospheric/(R.*temp);
velocity = sqrt(2*q_transducer/rho); % Free-stream Velocity
C_p = deltaP/(.5*rho*(velocity^2)); % Finding Coefficient of Pressure
% Assign values to arrays
%velocity_array(i, j, k) = velocity;
CpArray = [CpArray; C_p];
xArray = [xArray; x];
end
C_l = -1/chord * trapz(CpArray * cos(pi()/180 * alpha_range(j)));
ClArray = [ClArray; C_l];
n1 = i-2;
figure(n1)
grid on
hold on
plot(xArray, CpArray, 'm')
set(gca, 'Ydir', 'reverse')
xlabel('x/c')
ylabel('C_p')
titleVar = ['C_p vs. x/c for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
n2=n1+3;
figure(n2)
plot(-6:2:24, ClArray, '-*k')
xlabel('alpha')
ylabel('Cl')
titleVar = ['Cl vs. alpha for Reynolds Number: ', num2str(i), 'e5'];
title(titleVar)
end
2 个评论
Voss
2024-3-28
You're welcome! If this solved the problem, please "Accept this Answer". Thanks!
I'm not sure why the axes titles would be wrong. Are the axes xlabels and ylabels correct?
The only thing I could recommend is to check whether you have a variable or another function called "title", by running the command
which -all title
on the command line. That, and maybe try closing all figures before running your code and see if that works.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!