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

 採用された回答

Adam Danz
Adam Danz 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 17 日

0 投票

"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 件のコメント

geeks g
geeks g 2019 年 5 月 17 日
sorry, Actually i didnt understand you well.
can you help me more.
Adam Danz
Adam Danz 2019 年 5 月 17 日
Do you have specific questions?
geeks g
geeks g 2019 年 5 月 17 日
編集済み: geeks g 2019 年 5 月 17 日
@Adam Danz
i use for exaple
set or get like
set(handles.uitable,'data',false );
this stetment , make me clear all chekboxes????
Adam Danz
Adam Danz 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 17 日
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.
geeks g
geeks g 2019 年 5 月 17 日
i will try
thank you very much
Adam Danz
Adam Danz 2019 年 5 月 17 日
No problem, let me know if you have further questions about this.
geeks g
geeks g 2019 年 5 月 17 日
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 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 17 日
What's the complete copy-pasted error message?
geeks g
geeks g 2019 年 5 月 17 日
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.
>>
Adam Danz
Adam Danz 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 17 日
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.
geeks g
geeks g 2019 年 5 月 17 日
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?
Adam Danz
Adam Danz 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 17 日
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 2019 年 5 月 17 日
編集済み: Adam Danz 2019 年 5 月 18 日
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);
Adam Danz
Adam Danz 2019 年 5 月 18 日
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 2019 年 5 月 18 日
編集済み: geeks g 2019 年 5 月 18 日
yay
yes its work
its stay one step and i finish it
geeks g
geeks g 2019 年 5 月 18 日
how could i use 'enable' in ui table
if i choose a specialty (IT) in university
i want some subjects to be non enable
Adam Danz
Adam Danz 2019 年 5 月 18 日
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.
geeks g
geeks g 2019 年 5 月 18 日
thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

2019 年 5 月 17 日

コメント済み:

2019 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by