Save double vector in text file
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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);
0 Kommentare
Antworten (2)
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
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.
.
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
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.
Siehe auch
Kategorien
Mehr zu Arithmetic Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!