How do I populate a custom table in a Simulink Mask?

15 views (last 30 days)
I'm looking to add a row in a custom table that’s in a mask. The name of the custom table is 'Table1', and I have the following code in one of the callbacks for a button on the mask:
Table1 = [Table1;cellstr(["a","b","c","d"])]
However, when I click on the button, I receive the following error:
Undefined function or variable Table1
How do I add a row to the custom table?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Jun 2019
Please note that mask callbacks operate in the Base Workspace. The mask parameters, however, are stored in the mask workspace. This is why you see an error stating 'Undefined function or variable Table1', as the callback does not have access to the mask workspace. To work around this, you would need to obtain the mask object and access the mask parameters within:
blkName = 'add_row_to_table_callback/Subsystem';
mask = Simulink.Mask.get(blkName);
You would then need to obtain the handle to the custom table in the mask and add the row as follows: Table1 = mask.getDialogControl('Table1');
I'm including a link to a documentation page that discusses ways to manipulate custom tables.

More Answers (1)

José M. Requena Plens
José M. Requena Plens on 13 Sep 2020
Edited: José M. Requena Plens on 14 Sep 2020
I don't understand how it can't work like a normal table. I leave here my code to be able to work with the table in Matlab or callbacks. (Demo at the end)
% This table example contain 3 columns, the fist column is a row names, and
% the others are checkbox data.
% | colTwo | colThree |
% -------------------------------
% Row1 | '1000' | 'left' |
% Row2 | '4000' | 'top' |
% Row3 | '6000' | 'right' |
% Block
blk = gcb;
% Extract table
tableChar = get_param( blk, 'table_blk' ); % table_blk is your table parameter
% Clean char array
tableCell = textscan(erase(tableChar,["{","}","'"]),'%s%s%s','Delimiter',',','EndOfLine',';');
% Get Columns data (in cells)
colTwo = tableCell{:,2};
colThree = tableCell{:,3};
% Create standard Matlab table from masked table data
tableSTD = table(colTwo,colThree,'Rownames',tableCell{:,1},'VariableNames',{'Col2','col3'});
% ====================================
% Work with standard Matlab table here
% ...
% Examples
tableSTD(1,1) = {'3000'}; % Change first variable for first row to 3000
tableSTD(3,2) = {'bottom'}; % Change second variable for third row to 'bottom'
tableSTD.Col2 % Get all values in column 2
tableSTD.Col2('Row1') % Get Row1 value in column 2
% ...
% ====================================
% Now convert table to char array in masked table format
str = '{';
numParams = height(tableSTD);
mark = char("'");
for ii = 1:numParams
colRowName = tableSTD.Row{ii};
colTwo = char(tableSTD{ii,1}); % Convert cell to char
colThree = char(tableSTD{ii,2});
str = [str,mark,colRowName,mark,',',mark,colTwo,mark,',',mark,colThree,mark];
if ii < numParams
str = [str,';'];
str = [str,'}'];
% Set new values in masked table
p = Simulink.Mask.get(blk);
param = p.getParameter('table_blk');
param.Value = str;
Mask to table:
Table to mask:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!