Saving Data In A For Loop Into An Array

This seems like a basic question, but I can't seem to figure it out.
I have data for multiple spirals and I want all the x,y and z co-ordinates for each spiral in seperate arrays. I cannot preallocate because I am not aware of the size before running the code.
Would really appreciate any help on how to save my co-ordinates for each spiral in seperate arrays. The code is pasted below
clc; clear; close all
spirals = (1:10:61);
dy = max(spirals);
indent = linspace(-255+dy, 255-dy,4);
num = 1:1:7;
cla
hold on
layerheight = [1 2 4 5];
for columns=1:4
for rows = 1:length(spirals)
center = -250 + (spirals(rows))*num(rows);
gap=layerheight(columns);
d=layerheight(columns);
r = spirals(rows);
z=(linspace(0,d,10000));
t=(40*pi/gap)*z;
x=round(r*cos(t)+center);
y=round(r*sin(t)+ indent(columns));
plot3(x,y,z,'o','MarkerSize',2)
% for counter = 1:length(z)
% rowdata(counter) = [x(rows) y(rows) z(counter)]; %this is how I thought of saving the data, but it doesn't work
% end
end
end
hold off
xlim([-255 255])
ylim([-255 255])
zlim([0 5])
grid on
xlabel('\it Increasing Radius \rightarrow')
ylabel('\it Increasing Layer Height \leftarrow')

 Réponse acceptée

Star Strider
Star Strider le 20 Août 2019
The easiest way would probably be to save them all in separate cell arrays, here ‘xc’, ‘yc’, and ‘zc’:
for columns=1:4
for rows = 1:length(spirals)
center = -250 + (spirals(rows))*num(rows);
gap=layerheight(columns);
d=layerheight(columns);
r = spirals(rows);
z=(linspace(0,d,10000));
t=(40*pi/gap)*z;
x=round(r*cos(t)+center);
y=round(r*sin(t)+ indent(columns));
plot3(x,y,z,'o','MarkerSize',2)
% for counter = 1:length(z)
% rowdata(counter) = [x(rows) y(rows) z(counter)]; %this is how I thought of saving the data, but it doesn't work
% end
xc{rows,columns} = x;
yc{rows,columns} = y;
zc{rows,columns} = z;
end
end
Experiment to get the result you want.

6 commentaires

Hans123
Hans123 le 20 Août 2019
wow that works perfectly! The purpose is to export the co-ordinates into a text file, but now that I have them in cells I could do it.
Thanks as always, Star Strider!
Star Strider
Star Strider le 20 Août 2019
As always, my pleasure!
Hans123
Hans123 le 20 Août 2019
Hi StarStrider, just a follow-up on my issue. As I said, I am trying to save the X, Y and Z coordinates into a text file as 3 columns. I am trying to use the writefile command, but I am getting the error message. Undefined function or variable 'writecell'.
I would appreciate if you could tell me what is going on, and how I can write the data in a text file
Star Strider
Star Strider le 20 Août 2019
I cannot find any reference to a ‘writefile’ function. The writecell function was introduced in R2019a.
I am not certain how you want to write them. Note that each cell array has 28 elements, each of which is a (1x10000) vector.
Each circle would be described as a (3x10000) matrix. It might be easiest to save each circle ‘X’, ‘Y’, and ‘Z’ vector to a separate sheet of a spreadsheet file (such as Excel), although saving them as their transposes (so a (10000x3) matrix for each) would likely be easier. That will significantly reduce any problems you may have in importing them again. Use the xlswrite function for that. This could easily be programmed in a loop, using the sprintf function to create the matrices for each circle, and the command strings for xlswrite.
Hans123
Hans123 le 13 Sep 2019
StarStrider! My apologies for getting back to you this late. I figure out the answer and I forgot that I reached out for you for help!
Again I am really sorry.
Thanks for your help, I always appreciate it!
Star Strider
Star Strider le 13 Sep 2019
No worries!
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by