How can I concatenate the arrays in loop ? Matlab

There is a loop;

for i = 1:n

X_rotate  = X.*cos(i*increment) - Y.*sin(i*increment);
X_rotateh = X_rotate./cos(deg2rad(helix_angle));
Y_rotate  = X.*sin(i*increment) + Y.*cos(i*increment);

Helix     = [X_rotateh(1:K1) ; Y_rotate(1:K1)];
fileID    = fopen('helix_values.txt', 'w');
fprintf(fileID,'%f %f\n ', Helix);
fclose(fileID);
end

X and Y are row vectors and their size depends on inputs. By the way, the iteration number n and the size of X and Y can be different. As I said, they depend on inputs.

When open the text file, there just exists the values of last iteration for X_rotateh and Y_rotate. I need to collect the values of X_rotateh and Y_rotate from first value to K1 th value of both for every iteration. I have tried to use cat command. It did not give what I want. On the other hand, I usually meet problems which are about length or size of arrays.

Those values should be in order in text file like;

%for first iteration;

X_rotateh(1) Y_rotate(1)

X_rotateh(2) Y_rotate(2)

.

.

X_rotateh(K1) Y_rotate(K1)

%for second iteration;

X_rotateh(1) Y_rotate(1)

X_rotateh(2) Y_rotate(2)

.

.

X_rotateh(K1) Y_rotate(K1)
%so on..

What may I do ?

Thanks in advance.

Upvotes: 1

Views: 206

Answers (1)

Umair47
Umair47

Reputation: 1142

As you said, the text file has results from the last iteration. It's probably because you are opening the text file with 'w' permission. Which writes the new content but also erases the previously stored content in the file. Try using 'a' permission. This will append new content without erasing previous content.

fileID = fopen('helix_values.txt', 'a');

You can also find more details with help fopen command in MATLAB.

Let me know if this solves the problem.

Upvotes: 1

Related Questions