GUI uitable logical values

SO I HAVE this ui table i want to select sujects and push 'submit' to calculate the sum of credit for the subject i selected
i dont know how to dealing with uitable to get data from it and calculat the sum
and after that i want to push a clear' button to reset all
help please

 Accepted Answer

Adam Danz
Adam Danz on 17 May 2019
Edited: Adam Danz on 17 May 2019
"SO I HAVE this ui table i want to select sujects and push 'submit' to calculate the sum of credit for the subject i selected"
In the callback function for the "submit" button, add this section of cose. You'll need to adapt it to your gui. That means you'll need to change the variable names and the column numbers.
% I create a fake UI table just for the example
h.uitable = uitable('Data', [{false;true;true;false;false},num2cell((1:5)')], 'ColumnEdit', [true, true],'ColumnName', {'Select','Credit'});
% In my table, the check boxes are in column 1, then credits are in column 2.
% * you'll need to adapt this to your table (the variable names and column numbers)
% * These steps could be combined into 1 line but it's more intuitive this way
% Step 1) Determine which rows have checked boxes
rowChecked = [h.uitable.Data{:,1}]';
% Step 2) Get the credits for each row of checked boxes
credits = [h.uitable.Data{rowChecked,2}]';
% Step 3) add the credits
creditSum = sum(credits);
"and after that i want to push a clear' button to reset all"
In the callback function to you "clear" button, add this section of code. Again, you'll need to adapt it to your GUI.
% set all checkboxes to false
h.uitable.Data(:,1) = {false};
% Remove all credits
h.uitable.Data(:,2) = [];

18 Comments

sorry, Actually i didnt understand you well.
can you help me more.
Do you have specific questions?
geeks g
geeks g on 17 May 2019
Edited: geeks g on 17 May 2019
@Adam Danz
i use for exaple
set or get like
set(handles.uitable,'data',false );
this stetment , make me clear all chekboxes????
Close, but not correct.
The set() version would be
data = get(h.uitable,'Data');
data(:,1) = {false};
set(h.uitable,'Data',data)
But if you can use the dot notation rather than get(), set(), you should.
i will try
thank you very much
No problem, let me know if you have further questions about this.
I WRITE LIKE YOU TELL ME
NOTHING WORK
AND THERE IS AN ERROE
this my code: after i adapt it to my GUI.
% Step 1) Determine which rows have checked boxes
rowChecked = [handles.uitable.Data{:,1}];
rowChecke = [handles.uitable.Data{:,4}];
% Step 2) Get the credits for each row of checked boxes
credits = [handles.uitable.Data{rowChecked,2}];
credit = [handles.uitable.Data{rowChecke,6}];
% Step 3) add the credits
creditSum = sum(credits);
creditSu = sum(credit);
a = creditSum +creditSu ;
Adam Danz
Adam Danz on 17 May 2019
Edited: Adam Danz on 17 May 2019
What's the complete copy-pasted error message?
i put this code in 'submit' button
then i run the program
its ok until now
when i push 'submit'
this error show up
------------------------------
>> Course_Selector_
Reference to non-existent field 'uitable'.
Error in Course_Selector_>pushbutton4_Callback (line 340)
rowChecked = [handles.uitable.Data{:,1}];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Course_Selector_ (line 63)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Course_Selector_('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
>>
The error message tells you what's wrong:
Reference to non-existent field 'uitable'.
There is no field in "handles" named 'uitable'.
1) Are you writing this in GUIDE or app designer (or something else)?
2) Is this code in the callback function to the submit button?
Please copy-paste the entire call-back function here and please format the code using the format button.
omg omg omg
its work its work
omg it work
but know how could i mov the result of sum AND put it in text?
Ha! Glad to hear it's working! :)
To display the result in your GUI, you need to convert it to a string and assign it to a text object. Let's say your text box is named "textfield".
handles.textfield.String = num2str(creditSum);
% or, to specify number of decimal places
handles.textfield.String = sprintf('%.1f',creditSum);
geeks g
geeks g on 17 May 2019
Edited: Adam Danz on 18 May 2019
look to this code please
is that ok??
% Step 1) Determine which rows have checked boxes
rowChecked = [handles.uitable1.Data{:,1}];
rowChecke = [handles.uitable1.Data{:,4}];
% Step 2) Get the credits for each row of checked boxes
credits = [handles.uitable1.Data{rowChecked,2}];
credit = [handles.uitable1.Data{rowChecke,6}];
% Step 3) add the credits
creditSum = sum(credits);
creditSu = sum(credit);
a = creditSum + creditSu ;
set(handles.text7,'string',a);
Does it work?
In my examples, I convert 'a' from number to string using one of the two methods below
  • num2str(a);
  • sprintf('%.1f',a);
geeks g
geeks g on 18 May 2019
Edited: geeks g on 18 May 2019
yay
yes its work
its stay one step and i finish it
how could i use 'enable' in ui table
if i choose a specialty (IT) in university
i want some subjects to be non enable
Read about the 'enable' property here:
When 'enable' is turned off, the entire table is deactivated. You can't apply this to certain cells. Instead, you could change the selection available whenever a new specialtiy is chosen. For example, whenever IT is chosen the table content could change to a new list.
thank you

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 17 May 2019

Commented:

on 18 May 2019

Community Treasure Hunt

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

Start Hunting!