how can i change the number formatting with writetable
38 views (last 30 days)
Show older comments
I am storing output in .csv files that I create using the writetable function. The non-integer values that go into the table are converted to strings with 6 decimals. My problem is that when doing this, I get leading spaces in front of NaN values in the .csv file. My question is, is it possible to still use the writetable function and specify the number format of the output (i.e., 6 decimals even for round numbers), and avoid leading spaces in from of NaNs?
Here is what I am currently doing:
% Data:
ID = [ 1; 2; 3 ];
Val = [ 0.7564; nan; 1 ];
% Convert number to string for formatting:
txt_val = num2str(Val,'%.6f');
% Create table
T = table (ID, txt_val, 'VariableNames', {'ID', 'Value'});
% Write table to |-delimited file
writetable(T, 'output.csv','Delimiter','|');
I have used dlmwrite for something similar before, but in my experience it takes ca 5 times longer than writetable. For this particular program, computing time is key as I will be writing millions of rows in one go. I am hoping that there is a way I can stick to the writetable method, but still get the number formatting right.
Thanks for any help!
2 Comments
Henry Herrera
on 18 Oct 2017
Edited: Henry Herrera
on 18 Oct 2017
Thank you Oystein, This works great. I just trimmed to the desired precision before writing the table to an excel file:
Var1trim = strtrim(cellstr(num2str(T.Var1,'%.2f')));
T.Var1 = Var1trim;
Thanks.
Answers (0)
See Also
Categories
Find more on Data Type Conversion 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!