Column Format in a Table when using writetable
조회 수: 34 (최근 30일)
이전 댓글 표시
Is there anyway to format certain columns in a table.
My table looks like this in the command window,
T =
10×5 table
count FrameNumber DT DiffDT Type
_____ ___________ ______ ______ ___________
1 0 0 0 {'Start' }
2 0 211.99 211.99 {'Trigger'}
3 1 304.85 92.857 {'Trigger'}
4 2 397.93 93.082 {'Trigger'}
5 3 489.56 91.628 {'Trigger'}
6 4 575.42 85.855 {'Trigger'}
7 5 674.97 99.555 {'Trigger'}
8 6 762.02 87.047 {'Trigger'}
9 7 852.58 90.566 {'Trigger'}
10 8 1070.6 218.01 {'Stop' }
But when i save it to file, its not quite aligned, Im assuming its because of the number of decimal points
count FrameNumber DT DiffDT Type
1 0 0 0 Start
2 0 211.992979049683 211.992979049683 Trigger
3 1 304.849863052368 92.8568840026855 Trigger
4 2 397.932291030884 93.0824279785156 Trigger
5 3 489.56036567688 91.6280746459961 Trigger
6 4 575.415849685669 85.8554840087891 Trigger
7 5 674.970626831055 99.5547771453857 Trigger
8 6 762.017250061035 87.0466232299805 Trigger
9 7 852.582931518555 90.5656814575195 Trigger
10 8 1070.59550285339 218.012571334839 Stop
I thought T.Columnformat would work but it doesn't
This is my code: (its from a video object event log)
function [T,count]=EventsLogTable(app,vid)
events = vid.EventLog;
% events.Data
% events.Type
Type = {events.Type}.';
Data = [events.Data];
AbsTime = datetime(vertcat(Data.AbsTime));
T0 = AbsTime(1); % same
DT = milliseconds(AbsTime-T0);
DiffDT=diff(DT);
DiffDT=[0;DiffDT]; %Pad otherwise 1 less in length
%DT.Format = 'mm:ss.SSSS'; % 'mm:ss.SSSS';
count = (1:numel(events)).';
FrameNumber = vertcat(Data.FrameNumber);
T = table(count,FrameNumber,DT,DiffDT,Type);
end
And then I call this:
[T,count]=EventsLogTable(app,vid);
writetable(T,'F:\Temp\TableT.txt','Delimiter','tab')
I have tried this to round the two columns I need to and its almost correct it.
DT = round(DT,2)
DiffDT = round(DiffDT,2)
count FrameNumber DT DiffDT Type
1 0 0 0 Start
2 0 168.4 168.4 Trigger
3 1 266.37 97.98 Trigger
4 2 356.95 90.58 Trigger
5 3 449.85 92.9 Trigger
6 4 655.42 205.56 Stop
How can I make the columns also have the same number of decimals i.e.
1 0 0 0 Start
2 0 168.40 168.40 Trigger
3 1 266.37 97.98 Trigger
4 2 356.95 90.58 Trigger
5 3 449.85 92.90 Trigger
6 4 655.42 205.56 Stop
Oh, and how to get the headings all over the columns correctly
댓글 수: 1
Walter Roberson
2025년 2월 18일
How can I make the columns also have the same number of decimals i.e.
1 0 0 0 Start
2 0 168.40 168.40 Trigger
I would point out that the zeros there do not have the same number of decimal points as the 168.40
답변 (2개)
Walter Roberson
2025년 2월 18일
편집: Walter Roberson
2025년 2월 18일
varfun calling compose specifying a character vector format (so getting out cell array of character vectors.)
Repeat as often as necessary for the different formats you want to apply to different columns. [] the columns together and index the result to reorder the columns as needed, ending up with a cell array of character vectors.
Now possibly pre-pend a header column.
Now use writecell
To make it explicit: there is no facility built into writetable() for specifying column format.
댓글 수: 0
Karan Singh
2025년 2월 21일
편집: Karan Singh
2025년 2월 21일
As Walter has already stated, "there is no facility built into writetable() for specifying column format", rest I also think that rounding off to a nearest decimal point would also not help.
I converted the numeric columns into strings using a function like "sprintf" or "num2str" with your desired format (for example, '%.2f' for two decimals). Once converted, I converted the table to the file, and the output has remained in the same format.
% Sample data (as in your command window output)
count = (1:10).';
FrameNumber = [0; 0; 1; 2; 3; 4; 5; 6; 7; 8];
DT = [0;
211.992979049683;
304.849863052368;
397.932291030884;
489.56036567688;
575.415849685669;
674.970626831055;
762.017250061035;
852.582931518555;
1070.59550285339];
DiffDT = [0;
211.992979049683;
92.8568840026855;
93.0824279785156;
91.6280746459961;
85.8554840087891;
99.5547771453857;
87.0466232299805;
90.5656814575195;
218.012571334839];
Type = {'Start'; 'Trigger'; 'Trigger'; 'Trigger'; 'Trigger'; ...
'Trigger'; 'Trigger'; 'Trigger'; 'Trigger'; 'Stop'};
% Create the table
T = table(count, FrameNumber, DT, DiffDT, Type);
% Display the original table in the command window
disp('Original Table T:');
disp(T);
% Write the original table to a file using default formatting.
writetable(T, 'Table_Original.txt', 'Delimiter', '\t');
disp('Wrote file "Table_Original.txt" using default numeric formatting.');
%% Modify numeric formatting for specific columns
% Since writetable does not let you specify a numeric format,
% we convert the numeric columns DT and DiffDT into strings with a fixed format.
T_modified = T; % make a copy of the original table
T_modified.DT = cellstr(num2str(T_modified.DT, '%.2f')); % fixed to 2 decimals
T_modified.DiffDT = cellstr(num2str(T_modified.DiffDT, '%.2f'));
% Display the modified table (now DT and DiffDT are strings)
disp('Modified Table T_modified:');
disp(T_modified);
% Write the modified table to a file.
writetable(T_modified, 'Table_Modified.txt', 'Delimiter', '\t');
disp('Wrote file "Table_Modified.txt" with DT and DiffDT formatted to 2 decimals.');
Here is the output to the file too attached.
Karan
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!