Can I write to the same csv file more than once in a script?

3 vues (au cours des 30 derniers jours)
Mel Smith
Mel Smith le 26 Avr 2015
My script is a game that has a menu where if the user picks the first option, there are multiple if, else, elseif, and for loops that follow and then they either win or lose and the game ends. The third option in the menu gives them the ability to look at the stats of the game as a csv file. The only problem I'm having is that MATLAB reads the matrix that contains the number of victories and defeats as undefined in option 3; I figured this was because the number of defeats and victories were saved in a separate loop preceding option 3. I thought about writing each element of the matrix to the csv file one at a time as the game proceeds but I'm unsure about whether or not this is possible. Even if it is, I'd have to extract it or refer to it in a completely different elseif loop - that is option 3, where:
elseif selection==3
easy_stats=[easy_victories easy_defeats];
medium_stats=[medium_victories medium_defeats];
hard_stats=[hard_victories hard_defeats];
stats=[1 easy_victories easy_defeats; 2 medium_victories medium_defeats; 3 hard_victories hard_defeats];
csvwrite('stats.csv',stats);

Réponse acceptée

Image Analyst
Image Analyst le 26 Avr 2015
csvwrite writes out the whole array you give it at that time, overwriting what was there before. It does not append. If you want to write out an element at a time, you can use fprintf(), just remember to call fclose() after you write out that element. It looks like you're somehow assembling all the game data into one array called stats and calling csvwrite(). As long as stats has everything your your needs to see, I think that should work fine. You might use system() to launch it with Excel or notepad if you want to make it convenient for them.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by