How to export data from a for loop without overwriting the data?

18 visualizaciones (últimos 30 días)
Julia Morandi
Julia Morandi el 24 de Jul. de 2018
Comentada: Julia Morandi el 24 de Jul. de 2018
I want to create a table with all of the x, y, and z coordinates and additionally all of the x2,y2,and z2 coordinates. Does anyone know how to do this so that the values don't overwrite within each for loop? My code is below...
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
d=1;
for t=(i-1)*e:2*pi/w:(n*2*pi)+e*(i-1)
d=d+1;
if mod(d,2)==0
x=(r+R)*sin(t);
y=(r+R)*cos(t);
z=(h/(n*2*pi))*(t-((i-1)*e));
plot3(x,y,z,'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
hold on
else
x=(r-R)*sin(t);
y=(r-R)*cos(t);
z=(h/(n*2*pi))*(t-((i-1)*e));
plot3(x,y,z,'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
hold on
end
hold on
end
hold on
end
for i=1:w/2
k=1;
for t2=(n*2*pi)+(e*(i-1)):-2*pi/w:(i-1)*e
k=k+1;
if mod(k,2)==0
x2=(r-R)*sin(t2);
y2=(r-R)*cos(t2);
z2=(h/(n*2*pi))*(t2-((i-1)*e));
plot3(x2,y2,z2,'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
hold on
else
x2=(r+R)*sin(t2);
y2=(r+R)*cos(t2);
z2=(h/(n*2*pi))*(t2-((i-1)*e));
plot3(x2,y2,z2,'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
hold on
end
hold on
end
hold on
end

Respuestas (1)

jonas
jonas el 24 de Jul. de 2018
You need to save the next entry as a new index:
out(ind)=1;
If you have steadily increasing i, then you can just write
for i=1:2
out(i)=1;
end
If you are looping over something less convenient, you can use a counter
counter=1;
for i=1.5:0.5:2.5
out(counter)=1;
counter=counter+1;
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by