Figures not showing up with for loop

9 views (last 30 days)
Lauren on 28 Mar 2024
Commented: Dyuman Joshi on 19 Apr 2024
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
Undefined variable 'data'.
3 CommentsShow 1 older commentHide 1 older comment
Lauren on 28 Mar 2024
Thank you
Dyuman Joshi on 19 Apr 2024

Voss on 28 Mar 2024
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
Error using plot
Specify the coordinates as vectors or matrices of the same size, or as a vector and a matrix that share the same length in at least one dimension.
Lauren on 28 Mar 2024
I changed that and got 6 plots this time, thank you so much for your help! Your plots' titles changed with their corresponding Reynolds numbers, but when I run it, all of my plots use 5e5. Do you know why this could be? The data appears different in each both so I am assuming it's just the title that's off.
Voss on 28 Mar 2024
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.

Categories

Find more on Annotations in Help Center and File Exchange

R2023a

Community Treasure Hunt

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

Start Hunting!