Why is my plot not working within my for loop?
Show older comments
I am trying to plot multiple helices. With the code I am using (below) the helix is plotting how I want it to, however, only one helix is not plotting. Does anyone know why this may be the case?
clc;
clear variables;
close all;
n = input('number of revolutions ');
r = input('radius of stent ');
a = input('angle with respect to the upwards horizontal in degrees ');
w = input('number of wires ');
R = input('radius of wire ');
h=(2*n*pi*r)/tand(90-a); %height of stent determined by other inputs
e = (2*pi)/(w/2); %theta (spacing between where each wire starts)
if mod(w,2) ==0 %number of wires is even
else
fprintf('ERROR:number of wires must be even'); %stops code if wire number is odd
return
end
if mod(n,1) ==0 %number of revolutions is whole number
else
fprintf('ERROR:number of revolutions must be whole number');
return
end
for i=1:w/2
for t = (i-1)*e:2*pi/w:(n*2*pi)+e*(i-1); %t value for CCW helices
if mod(t,4*pi/w)==0
x{i} = (r-R)*sin(t);
y{i} = (r-R)*cos(t);
z{i} = (h/(n*2*pi))*t(1);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
hold on
else
x{i} = (r+R)*sin(t);
y{i} = (r+R)*cos(t);
z{i} = (h/(n*2*pi))*t(11);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
hold on
end
hold on
end
hold on
end
5 Comments
Could you provide a set of input values so your code can be run?
Off the bat I have a suggestion. You are calling 'hold on' too many times. Prior to your loop you can create the figure and axis and hold it once. This isn't the source of the problem you described, though.
figure;
axh = axis();
hold(axh, 'on')
then you can get rid of all the other 'hold on' lines.
Julia Morandi
on 17 Jul 2018
dpb
on 17 Jul 2018
A guess would be
if mod(t,4*pi/w)==0
is never actually satisfied for most cases owing to FP rounding...
Julia Morandi
on 17 Jul 2018
Adam Danz
on 17 Jul 2018
Using those inputs, your code breaks in the 'else' section of your conditional at line
z{i} = (h/(n*2*pi))*t(11);
I'm guessing that t(11) is a mistake.
Accepted Answer
More Answers (1)
Julia Morandi
on 17 Jul 2018
0 votes
1 Comment
Adam Danz
on 17 Jul 2018
No problem, come back if you get stuck.
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

