How can I concatenate arrays in loop ?
Show older comments
Dear Contributers,
There is a loop;
for i = 1:n;
X_rotate = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate = X.*sin(i*increment) + Y.*cos(i*increment);
Helix = [X_rotate(1:K1) ; Y_rotate(1:K1)];
fileID = fopen('helix_values.txt', 'w');
fprintf(fileID,'%f %f\n ', Helix);
fclose(fileID);
end
When open the text file, there just exists the last values of iteration X_rotate and Y_rotate. I need to collect the values for every iteration. I have tried to use cat command but I probably made mistake. How may I do that?
Thanks in advance.
Accepted Answer
More Answers (1)
Star Strider
on 22 Dec 2015
I’m not certain what you’re doing, but to get the ‘X_rotate’ and ‘Y_rotate’ as vectors, you have to save them as such:
for i = 1:n;
X_rotate(i) = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate(i) = X.*sin(i*increment) + Y.*cos(i*increment);
end
I don’t know what their actual dimensions are, so you might have to add a second dimension to each:
for i = 1:n;
X_rotate(i,:) = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate(i,:) = X.*sin(i*increment) + Y.*cos(i*increment);
end
Do the file write after the loop, not in it.
5 Comments
Ender Rencuzogullari
on 22 Dec 2015
Star Strider
on 22 Dec 2015
Without knowing the sizes of the individual elements of ‘X_rotate’ and ‘Y_rotate’, I cannot write the appropriate loop code for it.
One option would be to save them as cell arrays:
for i = 1:n;
X_rotate{i} = X.*cos(i*increment) - Y.*sin(i*increment);
Y_rotate{i} = X.*sin(i*increment) + Y.*cos(i*increment);
end
Note the substitution of curly braces ‘{}’ for parentheses ‘()’.
Ender Rencuzogullari
on 22 Dec 2015
Star Strider
on 22 Dec 2015
I am not certain how your program is organised. However a while loop with a counter, or some other way to determine the size of the user’s inputs, for example asking the user, would be useful to define ‘n’.
Ender Rencuzogullari
on 22 Dec 2015
Categories
Find more on Multirate Signal Processing 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!