Tuesday, May 12, 2026

Matlab: Table on the PPT created via Matlab

% Created by LI Xu
% Version 1.0
% August 28, 2025


% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy
% http://lixuworld.blogspot.com/

clear;
clc;
close all;

timebegin=tic;
cur_data=date;
cur_time=fix(clock);
str1=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

import mlreportgen.ppt.*

% 1. Create a presentation
ppt = Presentation('AQ.pptx');

% 2. Create a slide (e.g., 'Blank' or 'Title and Content')
slide = add(ppt, 'Title and Content');
replace(slide, 'Title', 'Measured Data Table');

% 3. Prepare your data (Cell array: Rows x Cols)
data = {'Variable', 'Campus', 'Winnipeg'; 'Temperature (℃) ', '40', '15'; ...
    'Relative Humidity (%)', '65', '70'; ...
    'Wind speed (m/s)', '65', '70'; ...
    'PM2.5 (μg/m³)', '65', '70'; ...
    'PM10 (μg/m³)', '65', '70'; ...
    'NO₂ (ppb)', '65', '70'; ...
    'O₃ (ppb)', '65', '70'; ...
    'CO₂ (ppm)', '65', '70'};


% 4. Create and add the table
% 'Table' automatically creates rows/columns based on the data
tableObj = Table(data);
firstRow = tableObj.Children(1);

firstRow.Style = {BackgroundColor('#296080'), FontColor('white'), Bold()};
tableObj.Style = {HAlign('center'), VAlign('middle'), Border('solid'), RowHeight('0.3in')};
% tableObj.ColWidths = {'1.5in', '1.0in', '1.0in'};
% Column 3: 1.0 inches (Winnipeg)
spec1 = ColSpec('2.5 in');
spec2 = ColSpec('1.5 in');
spec3 = ColSpec('1.5 in');

% 2. Assign the array of specs to the table
tableObj.ColSpecs = [spec1, spec2, spec3];


% 1. Target the specific cell (Row 2, Column 2) using entry(row, col)
targetCell = entry(tableObj, 2, 2);

% 2. Apply the background color
targetCell.Style = {BackgroundColor('red')};

% 1. Target the specific cell (e.g., Row 2, Column 2)
targetCell = entry(tableObj, 2, 2);

% 2. Apply the font color (e.g., red)
% You can use names like 'red', 'white', or hex codes like '#FF0000'
targetCell.Style = [targetCell.Style, {FontColor('white')}];


% Add the table to the content placeholder
replace(slide, 'Content', tableObj);

% 5. Close and View
close(ppt);
rptview(ppt);



fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');
cur_data=date;
cur_time=fix(clock);
str2=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time End: ');
disp(str2);
timespan=toc(timebegin);
fprintf('Time Span: %.4f s\n', timespan);

disp('*************************************************************************');