How to write the output of the code to .csv format and save the file

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

Risposte (1)

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 Commenti

Could you please elaborate a litttle.
How can change the output such that it is easier for me to write it on .csv file
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);
Okay, but how do i for 100000 data.
I have just shown 3 values in this for your reference.
COuld you please suggest?
Put it in a loop where you change the values each time.
Looping will take more time when we deal with millions of data.
Could you please suggest an alternate approach in coding ? I am really stuck with this
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.
yes i agree,
Let us take the same example suppose we have more number of output lines and we have need to have the following pattern in a .csv file
for example
Place, ivc, ivc, ivc, iac, orc, ahha, and so on
repeated place, t, f, f, f, f, f, f, and so on
temperature, 40,150,200,230,100,200, and so on
X, 0.3,0.3,0.3,2.3,1.3,0, and so on
Y, -1.5, -1.5, -2.4,2.3,0.3,0 and so on
filename :: / folder / filename.csv
t test :: A_B_C: location_gif: 1, t, t, t,t,t,t and so on
This is the content in the .csv file, but let us think we have many such data. these data are independent of each other
in that case how do we do it without the for loop?
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.
Sure, Will work on it and let you the progress
Thank so much, I got the results
I need one help, thats when i have
[fid1, msg1] = fopen('file1.csv', 'wt' );
fprintf (fid1, 'Temperature \t');
fprintf('\n');
fprintf (fid1, 'Value_n \t');
fprintf('\n');
fprintf (fid1, 'Value_p\t');
for
for
for
...
fprintf (fid, '% 3.0f \ n' , temp)
fprintf (fid1, '% 1.1f\t' , vnw);
fprintf (fid1, '% 1.1f\t' , vpw);
end
end
end
I get the output
Temperature VNW VPW-40 1.8 -1.5 0 1.2 -1.8 25 1.2 -1.8 60 1.2 -1.5 -40 1.2 -1.8 0 1.8 -1.2 25 0.9 -1.8 60 0.9 -1.8 -40 0.9 -1.8 0 0.9 -1.8 25 0.9 -1.8 60 0.9-1.5
in a single line, but I should get the values in 3 different lines
Temperature 40,0,25 and so on ----------
Value_n 1.8, 1.2,1.2 and so on ----------
Value_p -1.5,-1.8, -1.8 and so on ----------
How do I assign each Temperature values to only temperature, Value_n to VNW and Value_p to VPW ? please help
"but I should get the values in 3 different lines..."
I already explained about them in this comment:
Is something unclear?

Accedi per commentare.

Tag

Richiesto:

il 5 Giu 2020

Modificato:

il 8 Giu 2020

Community Treasure Hunt

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

Start Hunting!

Translated by