![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400734/image.jpeg)
Last else if graph not plotting
3 次查看(过去 30 天)
显示 更早的评论
if i==3600
hold on;
plot(x,C);
elseif i==10800
plot(x,C);
elseif i==36000
plot(x,C);
elseif i==36300
plot(x,C);
hold off;
end
In the above program, if i varies from 0 to 36300 or more then last plot is not happening and if I add one more else if i ==36600 then that will not get plot.
0 个评论
采纳的回答
S. Walter
2020-11-2
编辑:S. Walter
2020-11-2
I tried the following:
% Clear the command window and close the figures
clc
close all
% Make a color vector for plots
col_vec = lines(4);
% Start a counter for the legend entry
cnt = 1;
% Loop over the entries
for i = 1 : 1 : 36300
% Make random data
x = 0:1:10;
C = rand(size(x));
% If/else statements - added some extra code for plotting clarity
if i==3600
hold on;
plot(x,C,'-o','MarkerFaceColor',col_vec(cnt,:))
leg{cnt} = 'first';
cnt = cnt+1;
elseif i==10800
plot(x,C,'-d','MarkerFaceColor',col_vec(cnt,:))
leg{cnt} = 'second';
cnt = cnt+1;
elseif i==36000
plot(x,C,'-s','MarkerFaceColor',col_vec(cnt,:))
leg{cnt} = 'third';
cnt = cnt+1;
elseif i==36300
plot(x,C,'-^','MarkerFaceColor',col_vec(cnt,:))
leg{cnt} = 'fourth';
hold off;
end
end
% Annotate the plot
legend(leg,'location','northeast')
axis([0 10 0 1.1])
grid on
xlabel('x')
ylabel('C')
When I do that, I get four lines with markers
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400734/image.jpeg)
However, as the other commenter said, placing hold on and hold off ahead of the if/else statements is more robust, because if the condition is not met, you might not get the right hold conditions.
3 个评论
S. Walter
2020-11-2
I tried to run your code, setting the following variables:
% The calculation length domain (in m)
lengt = 1;
% No. of grid points
points = 100;
deltaX = (lengt./(points-1));
% Time step (in seconds)
deltat = 1;
% Diffusivity
diff = 1e-5;
% Maximum time (in seconds)
t = 370000;
And I get the following result
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400789/image.jpeg)
It's a bit difficult to see but the result of i = 36000 and i = 36300 are basically on top of each other. Below is a picture of the result zoomed in to axis([49 51 0.57 0.59]):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400794/image.jpeg)
Just as a note, you may want to evaluate how you are doing your time step. You currently have your
and iteration in time as the same thing but your iterations must be integers. This line:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/400799/image.png)
i = 0:deltat:t
更多回答(1 个)
KSSV
2020-11-2
Try like this:
hold on
if i==3600
plot(x,C);
elseif i==10800
plot(x,C);
elseif i==36000
plot(x,C);
elseif i==36300
plot(x,C);
end
hold off
Though I don't understand..what is the purpose to plot like this.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!