How to write the output of the code to .csv format and save the file
Show older comments
place = ivc, val_a = 3.0000, close_a = 3.2806, diff_a = -0.0935, val_b = 3.0000, close_b = 2.8365, diff_b = 0.0545 X = 0.3, Y = -1.5, temp = 40
place = ivc, val_a = 3.0000, close_a = 3.1910, diff_a = -0.0637, val_b = 3.0000, close_b = 2.6977, diff_b = 0.1008 X = 0.3, Y = -1.5, temp = 150
place = ivc, val_a = 3.0000, close_a = 2.5851, diff_a = 0.1383, val_b = 3.0000, close_b = 2.9607, diff_b = 0.0131 X = 0.0, Y = -2.4, temp = 200
This is the output of my code has more than 10000 entries, but only shown 3 lines for reference. now i want it to be in this .csv format as follows
Place, ivc, ivc, ivc
repeated place, t, f, f, f
temperature, 40,150,200
X, 0.3,0.3,0.3
Y, -1.5, -1.5, -2.4
filename :: / folder / filename.csv
t test :: A_B_C: location_gif: 1, t, t, t
% Description
Place: There we enter the place
Repeated place: t - is true, just add f for all the places that you add (since there are 3 places we have 3 times f)
Temperature: is the temperature (in this example -40 150 200)
X and Y as separate variables
filename: add the path of the file
t test :: A_B_C: location_gif: 1, t, t, t,
The number of "t" s corresponding to the number of places.
So how do i write it? and also how do i link the output of the code and save it in .csv format? Kindly help
Answers (1)
Image Analyst
on 6 Jun 2020
0 votes
Because of all that other stuff you want (place, temperature, etc.) I think it's best if you just use fprintf() to write all that stuff out in your complicated custom format.
11 Comments
Ganesh Kini
on 6 Jun 2020
Image Analyst
on 6 Jun 2020
For example:
fullFileName = fullfile('Delete Me.csv');
fid = fopen(fullFileName, 'wt');
if fid == -1
return;
end
fprintf(fid, 'Place, ivc, ivc, ivc\n');
fprintf(fid, 'repeated place, t, f, f, f \n');
fprintf(fid, 'temperature, %d, %d, %d\n', 40,150,200);
fprintf(fid, 'X, %.1f, %.1f, %.1f\n', 0.3,0.3,0.3);
fprintf(fid, 'Y, %.1f, %.1f, %.1f\n', -1.5, -1.5, -2.4);
fprintf(fid, 'filename :: %s\n', fullFileName);
fprintf(fid, 't test :: A_B_C: location_gif: 1, t, t, t\n');
fprintf(fid, '%% Description\n');
fprintf(fid, 'Place: There we enter the place \n');
fprintf(fid, 'Repeated place: t - is true, just add f for all the places that you add (since there are 3 places we have 3 times f)\n');
fprintf(fid, 'Temperature: is the temperature (in this example -40 150 200)\n');
fprintf(fid, 'X and Y as separate variables \n');
fprintf(fid, 'filename: add the path of the file \n');
fprintf(fid, 't test :: A_B_C: location_gif: 1, t, t, t,\n');
fprintf(fid, 'The number of "t" s corresponding to the number of places.\n');
fclose(fid);
Ganesh Kini
on 6 Jun 2020
Image Analyst
on 6 Jun 2020
Put it in a loop where you change the values each time.
Ganesh Kini
on 6 Jun 2020
Image Analyst
on 6 Jun 2020
It will take more time than a single line of text would of course. But I can loop millions of lines in a few seconds. I can't do anymore without a sample of what you'd like the output to be and what your data is. Your original post didn't really show that well.
Ganesh Kini
on 6 Jun 2020
Image Analyst
on 6 Jun 2020
Attach a small section of an actual CSV file, say with just a hundred or two hundred lines so I can see the pattern. In addition, attach in a .mat file the variables for temperature, X, Y, etc.
Did you not understand how I used fprintf()? You might want to study up on format specifiers. They are a VERY IMPORTANT thing to know about since they're used in fprintf(), sprintf(), textscan(), etc. and you need to know about them, like when to use %f or %8.3f or %d or %s or whatever. Once you understand that I'm sure you will be able to make up all your fprintf()'s yourself. So invest a little time. I'll check back later today for the attachments, and if you say you just can't understand how to sue fprintf(), I'll help.
Ganesh Kini
on 6 Jun 2020
Ganesh Kini
on 8 Jun 2020
"but I should get the values in 3 different lines..."
I already explained about them in this comment:
Is something unclear?
Categories
Find more on Solver Outputs and Iterative Display 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!