App Designer - help with syntax for referring to an array's indices

Hi folks,
I'm trying to initialise the table values on the app designer to zero, then increment the relevant cell based on specific button clicks.
I'm doing this by making a general function ButtonPressed(), that increments the relevant index value of the array of numbers that populates the table, then having each button calling this function when pressed, but for the index relevant to it.
I'm certain that my syntax is incorrect, but I can't seem to find any documentation on this and would appreciate some help if possible!
Below are my code and a screenshot of the app.
Thanks!
methods (Access = private)
function ButtonPushed(app, index)
app.dataStructCounts(index) = app.dataStructCounts(index) +1;
app.dataStructPercentage(index) = (app.dataStructPercentage(index) / app.TotalCounts) * 100;
app.TotalCounts = (app.IncipientCounts + app.CircularCounts + app.LenticularCounts + app.RibbonCounts + app.IsotropicCounts + app.FillerCounts);
app.TotalPercentage = (app.IncipientPercentage + app.CircularPercentage + app.LenticularPercentage + app.RibbonPercentage + app.IsotropicPercentage + app.FillerPercentage);
app.UITable.Data = [app.dataStructCounts, app.dataStructPercentage];
end
end
function startupFcn(app)
app.dataStructCounts = {app.IncipientCounts; app.CircularCounts; app.LenticularCounts; app.RibbonCounts; app.IsotropicCounts; app.FillerCounts; app.ResinCounts; app.TotalCounts};
app.dataStructPercentage = {app.IncipientPercentage; app.CircularPercentage; app.LenticularPercentage; app.RibbonPercentage; app.IsotropicPercentage; app.FillerPercentage; app.ResinPercentage; app.TotalPercentage};
app.TotalCounts = (app.IncipientCounts + app.CircularCounts + app.LenticularCounts + app.RibbonCounts + app.IsotropicCounts + app.FillerCounts);
app.TotalPercentage = (app.IncipientPercentage + app.CircularPercentage + app.LenticularPercentage + app.RibbonPercentage + app.IsotropicPercentage + app.FillerPercentage);
app.UITable.Data = [app.dataStructCounts, app.dataStructPercentage];
end
function UITableCellEdit(app, event)
indices = event.Indices;
newData = event.NewData;
app.UITable.Data = [app.dataStructCounts, app.dataStructPercentage];
end
% Button pushed function: IncipientButton
function IncipientButtonPushed(app, event)
ButtonPushed(app, 1);
end
end

 Accepted Answer

The question is mostly clear and the image is helpful. Interesting approach to align the buttons with the UITable rows - I like it. Your use of a general callback function assigned to all buttons is also well organized.
Since there are many variables in your code and we can only guess what they are, here's the general approach and you can let us know how it differs from what you're doing.
  1. The startup function should initialize the table with what appears to be 8x2 matrix of 0s (7 buttons + total). That would look like app.UITable.Data = zeros(8,2);
  2. Assuming the index input to the ButtonPushed function is the row number, you just need to gather the row of values into a 1x2 vector and assign it to the correct row.
newData = [app.dataStructCounts, app.dataStructPercentage];
app.UITable.Data(index,:) = newData;

6 Comments

@Adam Danz thank you for this, it's excellent! I tried implementing the second point and I get the following error, below. I tried stating app.datastructCounts(index).data but it came up with the error that dot notation wasn't allowed for variables of that type. Apologies for this, but is there a way around this please?
Disregard my previous comment (deleted). I just realized the error message is all the way to the right in your image.
It appears that app.dataStructCounts is a cell array. Try using curly brackets,
app.dataStructCounts{index} = app.dataStructCounts{index} + 1;
If that works, you'll need to apply the curly brackets to the other cell indexes.
The question becomes, why is this a cell array? It looks like it should be a numeric vector.
@Adam Danz Thanks, this works, but I think you're right that fundamentally this needs to be a vector and not a cell array! I'm not familiar with Matlab/app designer so I'm fumbling my way through...could you advise me on what this might look like please?
For all of the cell arrays that should be vectors such as app.dataStructCounts, could you show me or explain where/how they are created?
A cell array that should be a vector looks like this,
% x =
% 1×5 cell array
% {[1]} {[2]} {[3]} {[4]} {[5]}
These types of variable are often created by indexing another cell array using parentheses rather than curly brackets. Here's an example,
data = num2cell(1:5)
data = 1x5 cell array
{[1]} {[2]} {[3]} {[4]} {[5]}
x = data(1:3) % another cell array
x = 1x3 cell array
{[1]} {[2]} {[3]}
y = [data{1:3}] % a numeric array
y = 1×3
1 2 3
@Adam Danz thank you, this worked a treat! I rewrote the function with a numeric array and it worked!
Nice! That's an important lesson to learn, to work with numeric arrays rather than cell arrays of scalar numeric values, whenever possible.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 10 Mar 2021

Commented:

on 10 Mar 2021

Community Treasure Hunt

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

Start Hunting!