Save double vector in text file

39 Ansichten (letzte 30 Tage)
Bruce Rogers
Bruce Rogers am 2 Jul. 2021
Kommentiert: Yongjian Feng am 2 Jul. 2021
Hello everyone,
I'm trying to save a double array with one row and 5 columns in a text file and then put a new line into the text file. In the next step I change the parameters and the new double array should be saved into the same file so that in the end the result will be a matrix with n rows and 5 columns. Is there a way to do this? The matrix should look like this later:
val1_vec1 val2_vec1 val3_vec1 val4_vec1 val5_vec1
val1_vec2 val2_vec2 val3_vec2 val4_vec2 val5_vec2
val1_vec3 val2_vec3 val3_vec3 val4_vec3 val5_vec3
...
This is my attempt, but it doesn't work out fine, it just keeps writing the new values at the end of the old ones.
Thanks for your help!
fileID = fopen('Mindestgeschwindigkeit.txt','a');
fprintf(fileID, "%d\n\r ", midval);
fprintf(fileID, "\n", midval);
fclose(fileID);

Antworten (2)

Star Strider
Star Strider am 2 Jul. 2021
Change the line that writes the vector to:
fprintf(fileID, "%d ", midval);
That should work, if you want to write each vector as a row vector.
However, the best (and most efficient) option is likely to save the data as a matrix, and then use writematrix to write it all at the same time.
.
  8 Kommentare
Bruce Rogers
Bruce Rogers am 2 Jul. 2021
Bearbeitet: Bruce Rogers am 2 Jul. 2021
Exactly how I showed you above, this is the resulting text file. I don't know sure how it really will look like, because I still haven't solved the problem.
The problem is, why I can't just save it as a total matrix, is that
clear;
would delete every variable and the matrix included. What I do looks like this:
  1. Press run button
  2. Matlab: Loads 5 different sine waves of a robot into Matlab
  3. Program calculates mean phase shift of every robot sine wave and theory sine wave and saves in an 1x5 double vector array
  4. Save this 1x5 double vector array in an text file and generate a new line
  5. After this, the program has finished and I change the file path of the INPUT data.
irl_cell = readtable("C:\Users\Bruce Rogers\Documents\RoboDK\halfpi\Roboterfahrten\RSI_Auswertung_halfpi\min_vel15_halfpi.txt");
from min_vel10.txt to min_vel15.txt for all 5 sine waves.
6. I press run button (as just said, clear; would delete the matrix)
7. Matlab loads 5 new different sine waves
8. Program calculates new mean phase shift
9. Matlab saves this 1x5 double vector array in the same text file that was generated before, BUT this
time, it should be saved in the same text file in the next line
Or for better understanding, here a flowchart attached:
The problem is, that the new line is not created, the result keeps getting stored after the first 1x5 vector, so in total its getting a 1x10, 1x15, 1x20, etc. in the text file.
The text file should have 6 rows (for example) and 5 (fixed value)
Star Strider
Star Strider am 2 Jul. 2021
I’m lost.
Don’t use clear, especially while the code is executing. It clears everything, making the code much less efficient.
Instead, save the vector and write it at the end.
I’m stopping here.
.

Melden Sie sich an, um zu kommentieren.


Yongjian Feng
Yongjian Feng am 2 Jul. 2021
How about write it as a matrix to a CSV file?
https://www.mathworks.com/matlabcentral/answers/281156-how-can-i-export-a-matrix-as-a-csv-file
  5 Kommentare
Bruce Rogers
Bruce Rogers am 2 Jul. 2021
Hi @Rik,
as you see in Star Striders comment I tried to explain a bit slower what the problem with storing it in a matrix is.
Yongjian Feng
Yongjian Feng am 2 Jul. 2021
@Bruce Rogers: If you want to insert new rows, read the CSV file back as a matrix first, extend the matix, and save it back into the CSV file.

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by