Save double vector in text file
Ä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);
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
Bruce Rogers
am 2 Jul. 2021
Star Strider
am 2 Jul. 2021
Please describe exactly what the code produces (posting it would help), what the problem is, the vector (or vectors) you want to save to the file, and what you want as the result.
I do not understand not being able to save the results. If you are using a for loop, indexing the array is straightforward, and if you are using a while loop, use a counting variable, then reset it, if necessary, with new entries into the loop. Preallocation may not be possible, however even without it, saving the results and writing them to the file at the end would be more efficient than what appears to be the current approach.
If the vectors are different lengths each time, save them to elements of a cell array, then write the cell array to the file using writecell.
.
Bruce Rogers
am 2 Jul. 2021
Bearbeitet: Bruce Rogers
am 2 Jul. 2021
Star Strider
am 2 Jul. 2021
I can’t run that, however that’s not important. I don’t see a loop or anything resembling it (any sort of recursion) anywhere in the code or function, so I can’t determine how it works.
The code change I originally suggested should do what you want. It worked correctly in a test I ran, however I don’t understand the reason that it doesn’t work with your code.
The entire revised part of the code is then:
fileID = fopen('Mindestgeschwindigkeit.txt','a');
fprintf(fileID, "%d ", midval);
fprintf(fileID, "\n", midval);
fclose(fileID);
What does the file actually look like with this change after your code completes?
.
Bruce Rogers
am 2 Jul. 2021
Star Strider
am 2 Jul. 2021
I’m not sure what I need at this point.
What I want is to see what ‘Mindestgeschwindigkeit.txt’ looks like.
Bruce Rogers
am 2 Jul. 2021
Bearbeitet: Bruce Rogers
am 2 Jul. 2021
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
0 Stimmen
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
am 2 Jul. 2021
Rik
am 2 Jul. 2021
Why don't you store the results in an array in your loop, and after that write the entire matrix in one go? That would be much easier.
Star Strider
am 2 Jul. 2021
That was my first suggestion as well.
I have no idea what the problem is.
Bruce Rogers
am 2 Jul. 2021
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.
Kategorien
Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
