1

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.

5
  • Try fileID = fopen('helix_values.txt', 'a') to append data to the file. Also, that fopen line probably shouldn't be in the loop Commented Dec 23, 2015 at 14:00
  • I moved fopen outside. You are right. However, It is still the same @Dan Commented Dec 23, 2015 at 14:07
  • But did you try the line of code I suggested. You need to use 'a' for append, not 'w' from write which actually overwrites the file. Commented Dec 23, 2015 at 14:10
  • Thank You @dan. It worked. However, I used fopen inside the loop. Because, I moved it outside the loop and it did not work. I wanted to let you know Commented Dec 23, 2015 at 14:25
  • Yes, you also have to take fclose out of the loop! open the file, the loop and append to it, then close the file. Commented Dec 23, 2015 at 14:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes It did solve the problem, lots of thanks. However, I used fopen command inside the loop. It did not work when I moved it outside. @Umair47
It's working now and it would also work with fopen outside the loop. As @Dan suggested, if you want to take fopen outside (before the loop starts), you would also have to put fclose outside (after the loop ends). In this way, the file would be opened before the loop starts, all the writing would be done inside the loop and after all the iterations are completed, the file would be closed by fclose.
I just want to learn if I use fopen inside the loop, is it bad for programming? if it is, why ? @Umair47
Yes it's better if you keep fopen and fclose outside. With the current program, the file is 'opened' and 'closed' in every iteration. This is obviously bad for efficiency. In this case, as it is a simple text file, so you wouldn't notice a thing. Otherwise, if it is a large data file, you would start to see the negative effects in the result of a time delay during execution of each iteration.
Thanks for the knowledge
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.