Save double vector in text file
조회 수: 22 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Star Strider
2021년 7월 2일
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
Star Strider
2021년 7월 2일
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
2021년 7월 2일
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
Yongjian Feng
2021년 7월 2일
@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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!