why does an error that says "Line 31: The operation or expression 'plot( [t,delta_h]);' has no evident effect." appear when trying to plot

8 views (last 30 days)
I'm trying to plot a graph of t and delta_h, but even though both are the same size (20), in the command window "Array indices must be positive integers or logical values." pops up, while in the code issus "Line 31: The operation or expression 'plot( [t,delta_h]);' has no evident effect." pops up.
Can someone explain why theses messages/errors happen and how to fix it?
function sem()
plot = 1;
max_num = 20;
T_h = zeros([1,max_num]);
T_c = zeros([1,max_num]);
delta_h = zeros([1,max_num]);
delta_c = zeros([1,max_num]);
T_h(1,1) = 100;
T_c(1,1) = 0;
k = 0.033;
c = 0.126;
A = 1;
x = 0.01;
t = 0:1:max_num-0.1 ;
m = 1000;
for (i = 2: (max_num) )
delta_h(1,i) = ( (-k .* A .* ( T_h(1,(i-1)) - T_c(1,(i-1))) ) ./ (x .* m .* c) );
delta_c(1,i) = ( ( k .* A .* ( T_h(1,(i-1)) - T_c(1,(i-1))) ) ./ (x .* m .* c) );
end
size(t)
size(delta_h)
if (plot == 1)
plot( [t,delta_h]);
hold on
plot( [t,delta_c]);
end
end

Answers (1)

Steven Lord
Steven Lord on 19 May 2023
You cannot call the plot function while a variable named plot exists in the workspace. Your attempts to do so are treated by MATLAB as attempts to index into that variable (which as the error indicates won't work because your data do not represent valid indices into a variable.) The reason Code Analyzer warns about the lines where you index into the plot variable (while trying to call the plot function) is because indexing into the variable but not assigning the result of that indexing operation to another variable does appear to have no impact.
To fix the root cause, rename your plot variable.
If your t data represents the x coordinates and delta_h or delta_c the y coordinates, you should also not concatenate them together in your plot call. Pass them in as separate input arguments.
Based on those mistakes I suspect you're new to MATLAB or haven't used it for a while. If that's the case I recommend you work through the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB or refresh your memory.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!