How to write to csv file in shortG format?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
slavnikp
am 7 Mai 2024
Kommentiert: slavnikp
am 8 Mai 2024
Hello,
I would like to save a Matlab table (first column strings, the rest are floating numbers) to a csv in the shortG format. I have set the format in my script and when I look at the table in the program, it looks just as it should. However, when I save the table to a csv file, the numbers increase their decimal places counts suddenly, forbidding numbers like -6.0247e-06 etc. How to overcome this, please?
Thank you all in advance.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 8 Mai 2024
Here is one approach that gives exactly the shortG format in your CSV file:
format long G
M = rand(3,5);
M(1) = -6.0247123456789e-06
F = @(v)formattedDisplayText(v,'NumericFormat','shortG');
S = regexprep(arrayfun(F,M),{'\s+','\.{3}'},'');
writelines(join(S,','),'mytest.csv')
Checking the file content:
type mytest.csv
3 Kommentare
Stephen23
am 8 Mai 2024
"However, it is very slow for larger matrix dimension."
Yes, it will be slow. If you want fast file writing then either use WRITEMATRIX or FPRINT (you can specify the format with e.g. fixed fractional digits or significant figures). But because your question specified that you wanted exactly short G format, then you will have to convert this to text using something like what I showed in my answer, which will generally be slower than writing numeric data directly to the file.
Basically your stated requirement forces you into have slow code.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!