MATLAB Answers

I keep getting undefined function when preallocating

1 view (last 30 days)
Jamil Dudhwala
Jamil Dudhwala on 20 Feb 2019
Edited: Thorsten on 21 Feb 2019
I am trying to create a for loop wherein the temperature that is outputted is then subbed back in, I have preallocated but i get the error undefined function '(index)' when it comes to the line: S1QAmb(index)=zeros(size(index)) %surface 1, ambient load. Since the size of the array will change with the size of the index, I did not think the (index) needed to be defined. Also, if any other mistakes are spotted, please do let me know. Thank you.
My script is below:
S1=1.7*1.2 %Surface area of surface 1
S2= 1.7*1.2 %Surface 2 area
S3= 1.7*1.2 %surface 3 area
S4= 1.7*1.2 %surface 4 area
S5= 1.7*1.2 %surface 5 area
S6= 1.7*1.2 %surface 6 area
S7= 1.7*1.2 %surface 7 area
S8= 1.7*1.2 %surface 8 area
S9= 1.7*1.2 %surface 9 area
S10= 1.7*1.2 %surface 10 area
S1QAmb(index)=zeros(size(index)) %surface 1, ambient load
S2QAmb(index)=zeros(size(index)) %surface 2, ambient load
S3QAmb(index)=zeros(size(index)) %surface 3, ambient load
S4QAmb(index)=zeros(size(index)) %surface 4, ambient load
S5QAmb(index)=zeros(size(index)) %surface 5, ambient load
S6QAmb(index)=zeros(size(index)) %surface 6, ambient load
S7QAmb(index)=zeros(size(index)) %surface 7, ambient load
S8QAmb(index)=zeros(size(index)) %surface 8, ambient load
S9QAmb(index)=zeros(size(index)) %surface 9, ambient load
S10QAmb(index)=zeros(size(index)) %surface 10, ambient load
QAmb_Total(index)=zeros(size(index)) %Total Ambient Load
for index=(1:30)
if (index)==1
%Ambient Load
S1QAmb(index)=S1*U*(Tout-+Tin(index)) %surface 1, ambient load
S2QAmb(index)=S2*U*(Tout-+Tin(index)) %surface 2, ambient load
S3QAmb(index)=S3*U*(Tout-+Tin(index)) %surface 3, ambient load
S4QAmb(index)=S4*U*(Tout-+Tin(index)) %surface 4, ambient load
S5QAmb(index)=S5*U*(Tout-+Tin(index)) %surface 5, ambient load
S6QAmb(index)=S6*U*(Tout-+Tin(index)) %surface 6, ambient load
S7QAmb(index)=S7*U*(Tout-+Tin(index)) %surface 7, ambient load
S8QAmb(index)=S8*U*(Tout-+Tin(index)) %surface 8, ambient load
S9QAmb(index)=S9*U*(Tout-+Tin(index)) %surface 9, ambient load
S10QAmb(index)=S10*U*(Tout-+Tin(index)) %surface 10, ambient load
%Total Ambient Load in Array Form
%changein air cabin temperature where deltat is timestep
%New car cabin temperature


Sign in to comment.

Accepted Answer

Thorsten on 20 Feb 2019
Edited: Thorsten on 20 Feb 2019
index = 1:30;
% Preallocate
S1QAmb = zeros(size(index)); % index has to be defined before it can be used
for i = index
S1QAmb(i) = S1*U*(Tout+Tin(i)); % you wrote -+; that's invalid syntax
% and so on
You should also consider to use an array for S
S = 1.7*1.2*ones(1, 10);
and compute all S values in one line in the for loop
SQAmb(i,:) = S*U*(Tout+Tin(i));
with SQAmb preallocated as a matrix
SQAmb = zeros(numel(index), numel(S));


Show 2 older comments
Steven Lord
Steven Lord on 20 Feb 2019
Thorsten's suggestion to avoid tucking an index into the variable name is a good one, and is the generally recommended best practice on Answers.
As an aside, this isn't technically correct.
% you wrote -+; that's invalid syntax
Writing something like "1-+2" is valid. It is perhaps confusing to read, but it is valid.
The unary plus operator has higher precedence (level 4 unless raising to a power is involved, in which case it's level 3) than the subtraction operator (level 6), so this is equivalent to "1-(+2)" which is 1-2 or -1. That's the same reason "1--1" returns 2 -- unary plus and unary minus are at the same precedence level.
Jamil Dudhwala
Jamil Dudhwala on 20 Feb 2019
Sorry to bother you guys, but it seems I keep getting the error "subscript must either be real positive integers or logicals".
This must be because I have started of from i = 1, hence i - 1 = 0 and thats where the problem is. I thought that my ' if ' loop woould have sold that issue, but it does not seem to be working.
Thank you,
My revised script is below:
S = 1.7*1.2*ones(1, 10);
Tout = 5
ma = 1
ca = 2
DTM = 1
deltat = 1
index = 1:30;
SQAmb = zeros(numel(index), numel(S));
QAmb_Total = zeros(size(index));
deltaTin = zeros(size(index));
Tin = zeros(size(index));
%for loop
for i = index
if (i)==1
Tin(i) = 20
Tin(i) = Tin(i-1)+deltaTin(i)
SQAmb(i,:) = S*U*(Tout-Tin(i));
%Total Ambient Load in Array Form
QAmb_Total(i) = sum(SQAmb(i,:))
%change in air cabin temperature where deltat is timestep
deltaTin(i) = (QAmb_Total(i)/((ma*ca)+DTM))*(deltat)
%New car cabin temperature
Tin(i) = Tin(i-1)+deltaTin(i)
Thorsten on 21 Feb 2019
The if clause is fine. The problem occurs in the last but one line. You need to handle the case i == 1 also for this line.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by