# First cell in array is empty

2 views (last 30 days)
Angelina Cha on 4 Dec 2019
Edited: Adam Danz on 6 Dec 2019
Why is my first cell array empty in my variable a_th?
I should be getting:
[.02*(10^-6);
22.5*(10^-6);
0]
Instead I am getting an empty cell [ ].
I am running this program on R2017a.
%% thermal expansion
% Givens
theta = [0,90];
theta = theta*(pi/180);
a1 = 0.02*(10^-6);
a2 = 22.5*(10^-6);
a12 = 0;
delT = -75;
% Transformation Matrix
for i = 1:length(theta)
T{i} = [m^2, n^2, 2*m*n;
n^2, m^2, -2*m*n;
-m*n, m*n, (m^2)-(n^2)];
end
% Thermal expansion and Thermal strains for all angles of ply
for i = length(theta)
a_th{i} = T{i}*[a1;a2;a12];
ex = a_th{i}(1)*delT; %ax*deltaT
ey = a_th{i}(2)*delT; %ay*deltaT
exy = a_th{i}(3)*delT; %axy*deltaT
e_th{i} = [ex;ey;exy];
end

Adam Danz on 4 Dec 2019
Edited: Adam Danz on 6 Dec 2019
oops! Typo... should be
for i = 1:length(theta)
% ^^
end
Two more unrelated tips:
1. Always pre-allocate your loop variables (shown below)
2. numel() is safer than length()
a_th = cell(size(theta)); % PRE-ALLOCATE
e_th = cell(size(theta)); % PRE-ALLOCATE
for i = 1:numel(theta) % USE NUMEL
. . .
end