Simulate radio buttons inside a UITable

9 visualizaciones (últimos 30 días)
Alexander Larking
Alexander Larking el 19 de Feb. de 2020
Respondida: T. Dunker el 1 de Dic. de 2020
Hello. Warning that I am sort of new to MatLab, so I'm not exactly sure how much info I should give.
So, I have a UITable in app designer. The first three columns are data, and the fourth is filled with checkboxes. The idea is that a user will check a box and the data from that corresponding row will be added to a different table and the data will be stored in a universal variable for later use.
My issue is that I want the check boxes to behave as radio buttons (i.e., only one box in the table can be checked at once). MatLab does have built in radio buttons, but they cannot be inserted into a table. I tried doing this by writing code to set the checkboxes to false except the one the user changed, but it didn't really work.
Here's a bit of experimenting I did to try to figure out what was happening:
I've tried two different codes that execute upon the user changing data in the table (i.e., when they check the checkbox). For the first, I set every checkbox to false, but then change the checkbox that the user changed to true.
app.UITable.Data(:,4) = {[false]}; %Sets every value in the fourth row to false
if newData == true %Only executes if the user is changing something to true
app.UITable.Data(indices(1),4) = {[true]}; %Sets the value the user changed back to true
end
This made no discernable change in how the app behaved, so I tried running the same code, but without the if statement that set the data back to true:
app.UITable.Data(:,4) = {[false]};
As I expected, this made it so that whenever I clicked a checkbox, it was immediately returned to false.
So, I tried running the same thing, except set everything to true instead of false.
app.UITable.Data(:,4) = {[true]};
I thought that this would make every box true, but instead it made the box I checked permanently true. So, it seems to me that, for some reason, the only box I am able to change with code is the box that the user changed.
So, I decided that I would try to remove all of the data from the fourth column and replace it with the checkboxes that I want. I tried
app.UITable.Data = removevars(app.UITable.Data,{'✓'});
And I got the following error:
Error using tabular/subsasgnParens (line 208)
Unrecognized table variable name '✓'.
Error in tabular/removevars (line 25)
a = a.subsasgnParens({':',vars},[],false,true);
(Note: this is not because of the weird column name, I tried it with a different variable and got the same error).
This is how I defined my table (in the startup function):
app.UITable.ColumnFormat = {'char', 'char', 'char', 'logical'};
app.UITable.ColumnName = {'Fraction','Factorization','Intuition','✓'};
app.UITable.ColumnEditable = [false false false true];
And this is how I organize my data and display it (executes upon a button being hit, along with other code that calculates the data to be displayed):
app.cbxA = cell(1); %makes sure app.cbx is a cell and not double
app.tdataA = tdata; %puts the data into a universal variable
for n=1:counter %|===============================
app.cbxA(n,1)={[false]}; %%%| Creates the checkbox array |
end %|===============================
yourdata =[app.tdataA app.cbxA]; %Puts the data and the checkboxes into the same array
yourdata = cell2table(yourdata); %Converts from cell to table
app.UITable.Data = yourdata; %Puts the data into the UITable
What am I doing wrong? I'd appreciate any help! Thank you!

Respuesta aceptada

T. Dunker
T. Dunker el 1 de Dic. de 2020
Your question matched what I was searching for, too. As I could not find some hint for a built-in feature. I tried a callback strategy. The code below seems to do it.
function radiotest()
Name = {'A'; 'B'; 'C'; 'D'; 'E'};
Data = [1 2 3 4 5]';
Choice = logical([1 0 0 0 0]');
h = uitable(uifigure);
h.Data = table(Name, Data, Choice);
h.ColumnEditable = [false true true];
h.CellEditCallback = @callback;
end
function callback(src, event)
idx = event.Indices;
if idx(2) == find(strcmp(src.Data.Properties.VariableNames, 'Choice'))
src.Data.Choice(:) = false;
src.Data.Choice(idx(1)) = true;
end
end

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by