Get label name of item in dropdown menu

I have 2 dropdown menus that allow the user to select which variables to plot on a figure. I would like to also update the labels of the plot according to the label name of the selected item. Is there a way to access the label name without passing an object to ItemsData (e.g. {["var1",1],["var2",2], ...})?
data = ones(1,length(table_cols)-1); % Dummy data
eng_perf = uitab(PlotTabGroup,'Title','Engine Perf.');
perf_plot = uiaxes(eng_perf,"Units","normalized","Position",[0 0.05 1 0.95]);
% table_cols contains the names of the items
xdrop = uidropdown(eng_perf,"Position",[10 5 100 20],'Items',table_cols(2:end),...
"Tag",'xdrop',"Value",table_cols(2),"ItemsData",data);
ydrop = uidropdown(eng_perf,"Position",[120 5 100 20],'Items',table_cols(2:end),...
"Tag",'ydrop',"Value",table_cols(3),"ItemsData",data);
xdrop.ValueChangedFcn = @(xdrop,event) update_perf_plot(xdrop,ydrop,perf_plot);
ydrop.ValueChangedFcn = @(ydrop,event) update_perf_plot(xdrop,ydrop,perf_plot);
function update_perf_plot(xdrop,ydrop,perf_plot)
perf_plot.XLabel.String = xdropSelectedName; % Replace this line
perf_plot.YLabel.String = xdropSelectedName; % Replace this line
plot(perf_plot,xdrop.Value,ydrop.Value)
end

Answers (1)

@John F - since you are passing the drop down objects into the callback, I think you could just do
function update_perf_plot(xdrop,ydrop,perf_plot)
perf_plot.XLabel.String = xdrop.Value;
perf_plot.YLabel.String = ydrop.Value;
% !! Not sure what the next line is supposed to do !!
% plot(perf_plot,xdrop.Value,ydrop.Value)
end
That would allow you to set the current selection from each drop down to the appropriate label. As mentioned in the above code, it isn't clear to me how these same two values (strings) are supposed to be used in your call to plot.
There may also be alternatives to passing the drop down objects into this callback. If you have nested your callback within the main code that creates the drop downs, then you can access them directly. For example,
function nestedFunctionExample
eng_perf = uifigure;
xdrop = uidropdown(eng_perf,"Position",[10 5 100 20],'Items',{'abc' 'def' 'ghi'},...
"Tag",'xdrop',"Value",'def');
ydrop = uidropdown(eng_perf,"Position",[120 5 100 20],'Items',{'abcd' 'defd' 'ghid'},...
"Tag",'ydrop',"Value",'defd');
xdrop.ValueChangedFcn = @(xdrop,event) update_perf_plot;
ydrop.ValueChangedFcn = @(ydrop,event) update_perf_plot;
function update_perf_plot()
fprintf('xdrop=%s\n', xdrop.Value);
fprintf('ydrop=%s\n', ydrop.Value);
end
end

5 Comments

If you notice I have passed some data in the "ItemsData" property of the dropdowns. So xdrop.Value returns the selected item's data. So plot(perf_plot,xdrop.Value,ydrop.Value) plots the data of the 2 selected items. So I need to not only update the labels but also use the appropriate data for the plot.
@John F - so wil the above ccode work to update the labels or have I misunderstood the problem? If I have misunderstood, could you illustrate with a short example?
@Geoff Hayes Sorry for not making it clear enough. My problem is that I would like to have access to the label and its corresponding value inside the update_perf_plot function. I was able to find the following temporary solution using the UserData property:
data = ones(1,length(table_cols)-1); % Dummy data
eng_perf = uitab(PlotTabGroup,'Title','Engine Perf.');
perf_plot = uiaxes(eng_perf,"Units","normalized","Position",[0 0.05 1 0.95]);
% table_cols contains the names of the items
xdrop = uidropdown(eng_perf,"Position",[10 5 100 20],'Items',table_cols(2:end),...
"Tag",'xdrop',"Value",table_cols(2),"UserData",data);
ydrop = uidropdown(eng_perf,"Position",[120 5 100 20],'Items',table_cols(2:end),...
"Tag",'ydrop',"Value",table_cols(3),"UserData",data);
xdrop.ValueChangedFcn = @(xdrop,event) update_perf_plot(xdrop,ydrop,perf_plot);
ydrop.ValueChangedFcn = @(ydrop,event) update_perf_plot(xdrop,ydrop,perf_plot);
function update_perf_plot(xdrop,ydrop,perf_plot)
itemsx = string(xdrop.Items);
itemsy = string(ydrop.Items);
sel_item_x = xdrop.Value;
sel_item_y = ydrop.Value;
indx = find(itemsx == string(sel_item_x));
indy = find(itemsy == string(sel_item_y));
perf_plot.XLabel.String = xdrop.Value;
perf_plot.YLabel.String = ydrop.Value;
plot(perf_plot,xdrop.UserData(indx),ydrop.UserData(indy))
end
However, I feel that there should be a simpler solution that doesn't force me to find the index of the data that I need to plot. If this doesn't exist, I think it should be implemented in a future Matlab release as this feels too simple for a built-in solution to not exist.
@John F - perhaps since you are mapping strings to numbers, then maybe you should try using a map object. For example,
>> myMap = containers.Map(["a","b","cd"],[1,2,3]);
>> myMap("cd")
ans =
3
You could then create two map objects, one for the x drop down and one for the y drop down. You would then do something like
perf_plot.XLabel.String = xdrop.Value;
perf_plot.YLabel.String = ydrop.Value;
plot(perf_plot,xDropMap(xdrop.Value),yDropMap(ydrop.Value))
where xDropMap and yDropMap are the containers that map the drop down elements to the appropriate value.
@Geoff Hayes This does work but since I need to update the data contained in the map, I would also have to pass the keys around in my app. So I will keep my solution as is. I appreciate the help though and I hope someone else finds it useful in the future!

Sign in to comment.

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 8 Apr 2022

Commented:

on 14 Apr 2022

Community Treasure Hunt

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

Start Hunting!