Significant figures in table
35 次查看(过去 30 天)
显示 更早的评论
Hello,
I am having this project where I have to make large amounts of data in to .csv files. To do this, as you see below, I am making a table, that I can later on export as a .csv file. That works perfectly, the problem is that I would like my data on the generated table to be presented at two decimal points, (i.e. not 1 but 1.00).
Here is the code that i have written to generate the table:
T={ 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'; 3.19 0.25 67.1 66.7 70.3 13.0 45.0 55.2; 3.11 0.92 63.4 65.0 69.5 15.2 27.5 36.0; 3.11 1.24 62.0 64.1 69.0 12.2 22.4 30.5; 3.10 1.73 61.0 62.7 68.5 11.2 19.3 25.6; 3.06 2.09 59.3 62.0 67.9 10.7 17.7 23.1; 3.05 2.18 57.8 60.7 67.0 9.9 16.4 21.6; 2.91 0.43 67.8 65.1 63.1 11.0 33.4 46.8; 2.90 0.64 68.3 64.4 62.4 10.5 26.4 39.3; 2.88 1.22 68.0 63.2 60.3 10.2 20.1 29.1; 2.24 1.73 67.7 62.5 59.4 11.2 18.6 25.7; 2.85 2.08 66.9 61.3 58.1 10.7 17.0 23.3; 2.84 2.18 65.9 60.1 56.8 9.3 15.3 21.1};
C=T(2:end,:); %Excluding Column Names
Initial_Data=cell2table(C); %Making Table
Initial_Data.Properties.VariableNames=T(1,:); %Insert Desired Heading
-------------
Thanks in advance for your help.
0 个评论
采纳的回答
Jan
2016-3-6
Do you need the redirection of the table object? You can create the CSV-file directly:
T = { 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'; ...
3.19 0.25 67.1 66.7 70.3 13.0 45.0 55.2; ...
3.11 0.92 63.4 65.0 69.5 15.2 27.5 36.0}; % Abbrev. test data
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file: %s', FileName); end
nCol = size(T, 2);
fmt = repmat('%s, ', 1, nCol);
fmt(end-1:end) = '\n';
fprintf(fid, fmt, T{1, 1:nCol});
fmt = repmat('%.2f, ', 1, nCol); % <- Determine the number format here!
fmt(end-1:end) = '\n';
Data = cell2mat(T(2:end, :)).';
fprintf(fid, fmt, Data);
fclose(fid);
2 个评论
更多回答(1 个)
Peter Perkins
2016-3-7
For numeric variables, tables respect the long/short precision of the current command window setting, but do so using either format g or format e. If your command window is set to format short, for example, a table will display using format short g. If your command window is set to format long g, a table will display using format long g. Etc.
You can also use format bank, which may solve your original question.
The real answer, though, is that tables are designed for more data manipulation than for pretty printing, so if your goal is to get a specific display to copy/past into a report, you are probably better off doing the sort of thing Jan suggests.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!