Creating a list to select graphs on figure

I want to create a list using uicontrol('Style','listbox') where a figure will pop up and the user can choose which data appears on the graph by control clicking different functions in the list and then them all appearing on the graph unless the user deselects it. Note: I will use 'Callback' and then previous functions that were already made (e.g. sin, cos).

4 Comments

Which part of it is causing a problem? You haven't shown any code so it's not really possible for anyone to answer (plus you never asked a question) without a lot of effort that may be totally in the wrong direction.
lst = uicontrol('Style', 'listbox',... 'String', {'sin(x)','Cos(x)'},... 'Max',4,'Min',1, ... 'Position', [200 200 95 50],... 'Callback', 'plottyy'); so plottyy is a function that plots a sin wave. The problem is Whether I click sin or cos in the list, it will plot the sine function. I want a way to callback different functions depending on which choice is selected in the list.
function mygraph
% Create a figure and axes
f = figure('Visible','off');
ax = axes('Units','pixels');
% Create pop-up menu
pbtn = uicontrol('Style', 'pushbutton',...
'String', {'sin(x)'},...
'Position', [400 350 75 25],...
'Callback', 'plottyy');
pcbtn = uicontrol('Style', 'pushbutton',...
'String', {'cos(x)'},...
'Position', [400 320 50 25],...
'Callback', 'plottyyc');
% Create push button
btn = uicontrol('Style', 'pushbutton', 'String', 'Clear',...
'Position', [20 20 40 20],...
'Callback', 'cla');
% Make figure visble after adding all components
f.Visible = 'on';
end For example, this will plot different functions (plottyy, plottyyc) depending on the buttons pushed on the figure.
I'm not sure why this wont work. When I choose something in the list, nothing happens.
lst = uicontrol('Style', 'listbox',...
'String', {'sin(x)','cos(x)'},...
'Max',4,'Min',1, ...
'Position', [420 360 95 50]);
ListBoxCheck = get(lst,'String');
if strcmp(ListBoxCheck,'sin(x)')
plottyy
elseif strcmp(ListBoxCheck,'cos(x)')
plottyyc
end

Sign in to comment.

Answers (1)

From what I could understand about the situation after reading the conversation in the comments is that you are unable to access the value selected in the listbox.
it is because ListBoxCheck is a cell array. Also, it is the same array that is entered by you in 'uicontrol' when you typed ` 'String', {'sin(x)','cos(x)'},... `. so the execution of the following command results in a '2×1 cell array'
if true
ListBoxCheck = get(lst,'String')
% command window output is as following
ListBoxCheck =
2×1 cell array
'sin(x)'
'cos(x)'
end
The Value property stores the row indexes of currently selected list box items.
if true
ListBoxCheck = get(lst,'Value')
% command window output is as following
ListBoxCheck =
1
end
To enable multiple select, you can go through the following documentation link and search for 'listbox' under section 'Specifying the Uicontrol Style'. This should be of good help to you for solving you querry.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 2 Jun 2017

Answered:

on 6 Jun 2017

Community Treasure Hunt

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

Start Hunting!