How can I save data to a CSV file in the format I want?
7 views (last 30 days)
Show older comments
Hello,
I have a simple for loop of length 'hsteps', I use a software package GRIND which is a tool for analysing dynamical models (dV/dt= dynamics of e.g., vegetation). In this for loop every loop the value of 'hmax' increases. This has an effect on the dynamics of a variable 'V', which I store with Vout=outfun('V'), this saves all values for V for each time step, so simulating 1000 time steps gives 1000 values for Vout.
For clarity:
'stabil 500' lets the model stabilise for 500 time steps without saving output, 'time -s' prevents continuous graphical output, 'ke' saves the last value of 'V'.
I want to store the data for V for each according value of 'hmax' to a CSV file, where each column should represent a 'hmax' value and the the column should contain the data from Vout. With the script below however, I do not get what I want.
This is what I want (columns corresponding to 'hmax' value for a certain loop, output for variable V from the simulations):
hmax=0.4 hmax=0.44 hmax=0.48
1 1 1
2 2 2
4 3 2
This is what I get:
1 1
2 2
4 0.4
1 1
2 2
3 0.44
1 1
2 1
2 0.48
This is the script I've come up with so far:
simtime 0 1000 1000
fid=fopen('testharvest.csv','a+');
for i=1:length(hsteps)
hmax=hlow+i.*(hhigh-hlow)./length(hsteps);
stabil 500
V=1e-3;
time -s
ke
Vout= outfun('V');
%save output to CSV file
fprintf(fid, '%6.3f\n %4.2f\n', Vout, hmax);
end
fclose(fid);
Thanks in advance for any help and/or suggestions!
0 Comments
Answers (1)
Cedric
on 28 Jan 2013
Edited: Cedric
on 29 Jan 2013
You cannot access a file in column. You have several options for achieving what you want, that will in principle all have the same structure: 1st you loop over all hSteps and store Vout into some array; 2nd you write the array to file by either looping over its rows, or by calling a specialized function that will do that for you. For example:
n_hSteps = ... ;
n_vOut = ... ;
Vout = zeros(n_vOut, n_hSteps) ;
for ii = 1:n_hSteps
Vout(:,ii) = ...
end
dlmwrite('testharvest.csv', Vout) ;
would first build the array Vout with all your vout stored in column; then dlmwrite would output the array into the CSV file.
You could also build/store a header first (there would be more efficient ways than building it with the loop, but let's keep it simple)..
n_hSteps = ... ;
n_vOut = ... ;
Vout = zeros(n_vOut, n_hSteps) ;
header = cell(1, n_hSteps) ;
for ii = 1:n_hSteps
hmax = ...
header{ii} = sprintf('hmax=%.1f', hmax) ;
Vout(:,ii) = ...
end
dlmwrite('testharvest.csv', header) ;
dlmwrite('testharvest.csv', Vout, '-append') ;
Cheers,
Cedric
3 Comments
Cedric
on 29 Jan 2013
Edited: Cedric
on 29 Jan 2013
Dear Bas,
I understand now why you had the output that you gave in the original question; fprintf was indexing Vout linearly, which lined up its columns. Your new version will do the same, which will lead to a single-column CSV file. You can definitely reshape its content when you read it, but there are a few limitations:
- You cannot store a header. So you have to remember hmax values or store them elsehwere.
- The CSV file cannot really be read by human eye unless dimensions are small.
- You need to remember the dimension and number of Vout matrices if you want to read the file properly.
- In principle, columns of the V_DATA that you get after the reshape are the initial Vout's indexed linearly. So you have to proceed to additional reshapes if you want to get back the initial Vout s, which again means that you have to know/remember the initial dimension.
Regards,
Cedric
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!