MATLAB Answers

How to create the datas of third uitable by selecting a data in 1st uitable and a data in 2nd uitable?

1 view (last 30 days)
sachin narain
sachin narain on 25 Apr 2018
Commented: Ameer Hamza on 27 Apr 2018
I have three uitables(Tracks, Profil ,Markings)formed in Matlab Guide .I have column called Location in the 1st UItable.Location has 3 datas:'City','Country' and' Highway'.In another Uitable i have a column called number of ways which has data like :1,2,3,4 .If the user selects Location as Highway and number of ways 1 the third uitable should contain a column 'Direction' which has some value.If the user chooses Location as 'City' and number of ways as 2 the 'Direction'column in the third uitable should have some other value .Similary there can be different cases.
Till now i am able to apply a cellSelection fucntion to the 1st table and then when the user selects a cell inside 'Location', i find the cell indices where the column 'Location' is,but now how do i check what the user selects in the 2nd Uitable(since these are two seperate tables). After he selects the cell in the 2nd Uitable ,how do i create the 3rd table marking with the column direction with the required values
I will attach pictures of the Uitables along with this. Kindlt help me with this.Urgent need of a solution.
Thank you in advance

  1 Comment

Walter Roberson
Walter Roberson on 26 Apr 2018
Duplicates earlier which also has an answer.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 25 Apr 2018
There are several ways to communicate information between different UI elements. Here I am describing one of them. The key point is to understand the Parent and Children relation between MATLAB graphics objects. In your example, three tables are the Children of the figure window in which they are located. The easiest way for communication among several Children object is through parent object (in your case the figure window). Fortunately, in MATLAB, each graphics Object has a property called UserData, which MATLAB never uses and it is there to allow a user to store its own data. Understanding this. Here is a basic sketch what you can do,
table1 = uitable(...); % table1 is handle to first table
table2 = uitable(...); % file ... according to your table contents
table3 = uitable(...);
f = table1.Parent; % table2.Parent; table3.Parent; are all valid, since all have same parent
f.UserData.tab1 = table1; % populate the 'UserData' field with table handles.
f.UserData.tab2 = table2; % now all tables can access other like
f.UserData.tab3 = table3; % this: table1.Parent.UserData.table3.Data . Here table 1 is accessing table 3 Data field.
Now the next issue is how can we know, which cell of each table is selected. Here we will use CellSelectionCallback. The callback function will also set some fields in UserData property of figure f so that other tables can also access it. It can be done like this
f.UserData.tab1CurrentCell = [];
f.UserData.tab2CurrentCell = [];
f.UserData.tab3CurrentCell = [];
and then define callbacks like this
function table1CellSelectionCallBack(HObj, event)
% This function will be called when a cell on table 1 is selected. Therefore first we
% should save the value in 'UserData' so that other table can find updated value when they check.
HObj.Parent.UserData.tab1CurrentCell = event.Indices;
% to know current selection of table 2 and 3 do
currentSelectTable2 = HObj.Parent.UserData.tab2CurrentCell;
currentSelectTable3 = HObj.Parent.UserData.tab3CurrentCell;
% do whatever computation you want
% to change the value of some other table do
HObj.Parent.UserData.tab3.Data = []; % write value to table 3.
Similar callbacks can be written for table 2 and table 3 CellSelectionCallback. For more details on this topic, refer to my this answer.


sachin narain
sachin narain on 27 Apr 2018
OK.I understand.i am attaching how i have written it.Can you confirm it once more.thank you
handles.tab1CurrentCell = eventdata.Indices;
% to know current selection of table 2 and 3
switch data{handles.tab1CurrentCell(3)}; % Location column
case 'Highway'
switch handles.tab2CurrentCell(2);
case '2'
dir = {'inverse,inverse,direct,direct','direct,direct,inverse,inverse'} ;
case '3'
dir = {'inverse,inverse,inverse,direct,direct,direct','direct,direct,direct,inverse,inverse,inverse'} ;
case '4'
dir = {'inverse,inverse,inverse,inverse,direct,direct,direct,direct','direct,direct,direct,direct,inverse,inverse,inverse,inverse'};
% handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Executes when selected cell(s) is changed in table_profils.
function table_profils_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to table_profils (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
data =get(hObject,'Data');
handles.tab2CurrentCell = eventdata.Indices;
Ameer Hamza
Ameer Hamza on 27 Apr 2018
@Sachin What is display in the command window. Does it change when you select different cells in table 1 and 2.
Also, remember to add the line
guidata(hObject, handles);
to the second callback. In your last comment, it is not there.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!