Clear Filters
Clear Filters

Linking two list boxes and a push button for plotting graph.

6 views (last 30 days)
Hi, I need to link two list boxes containing different strings and a push button in Matlab GUI where the push button will instruct the graph to display the selected string from either of the list boxes. Attached below is my code.
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI
% Last Modified by GUIDE v2.5 03-May-2021 22:00:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in CList.
function CList_Callback(hObject, eventdata, handles)
a = get(handles.CList,'Value');
if(a==1)
set(handles.SongDisplay,'String');
elseif(a==2)
set(handles.SongDisplay,'String');
elseif(a==3)
set(handles.SongDisplay,'String');
elseif(a==4)
set(handles.SongDisplay,'String');
elseif(a==5)
set(handles.SongDisplay,'String');
elseif(a==6)
set(handles.SongDisplay,'String');
elseif(a==7)
set(handles.SongDisplay,'String');
end
% hObject handle to CList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns CList contents as cell array
% contents{get(hObject,'Value')} returns selected item from CList
% --- Executes during object creation, after setting all properties.
function CList_CreateFcn(hObject, eventdata, handles)
% hObject handle to CList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Confirm.
function Confirm_Callback(hObject, eventdata, handles)
% hObject handle to Confirm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Display.
function Display_Callback(hObject, eventdata, handles)
% hObject handle to Display (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Reset.
function Reset_Callback(hObject, eventdata, handles)
% hObject handle to Reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in D.
function D_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
set(handles.CList,'Visible','on')
%???????????????“??”
else
set(handles.CList,'Visible','off')
%?????“???”
end
% hObject handle to D (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of D
% --- Executes on button press in Short.
function Short_Callback(hObject, eventdata, handles)
% hObject handle to Short (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of Short
% --- Executes on slider movement.
function SizeSlider_Callback(hObject, eventdata, handles)
% hObject handle to SizeSlider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function SizeSlider_CreateFcn(hObject, eventdata, handles)
% hObject handle to SizeSlider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in WaveformReset.
function WaveformReset_Callback(hObject, eventdata, handles)
% hObject handle to WaveformReset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Play.
function Play_Callback(hObject, eventdata, handles)
% hObject handle to Play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Pause.
function Pause_Callback(hObject, eventdata, handles)
% hObject handle to Pause (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Stop.
function Stop_Callback(hObject, eventdata, handles)
% hObject handle to Stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in DList.
function DList_Callback(hObject, eventdata, handles)
a = get(handles.DList,'Value');
if(a==1)
set(handles.SongDisplay,'String');
elseif(a==2)
set(handles.SongDisplay,'String');
elseif(a==3)
set(handles.SongDisplay,'String');
elseif(a==4)
set(handles.SongDisplay,'String');
elseif(a==5)
set(handles.SongDisplay,'String');
elseif(a==6)
set(handles.SongDisplay,'String');
elseif(a==7)
set(handles.SongDisplay,'String');
end
% hObject handle to DList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns DList contents as cell array
% contents{get(hObject,'Value')} returns selected item from DList
% --- Executes during object creation, after setting all properties.
function DList_CreateFcn(hObject, eventdata, handles)
% hObject handle to DList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in CMaj.
function CMaj_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
set(handles.CList,'Visible','on')
else
set(handles.CList,'Visible','off')
end
% hObject handle to CMaj (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of CMaj
% --- Executes on button press in DMaj.
function DMaj_Callback(hObject, eventdata, handles)
if get(hObject,'Value') == 1
set(handles.DList,'Visible','on')
else
set(handles.DList,'Visible','off')
end
% hObject handle to DMaj (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of DMaj
% --- Executes on button press in Add.
function Add_Callback(hObject, eventdata, handles)
% hObject handle to Add (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function SongDisplay_CreateFcn(hObject, eventdata, handles)
% hObject handle to SongDisplay (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate SongDisplay
  1 Comment
J. Alex Lee
J. Alex Lee on 4 May 2021
i would discourage use of the GUIDE if you have a recent release of matlab...
the "uidropdown" element has data and display properties so you shouldn't have to manually do what you're doing, so something like
function DList_Callback(hObject, eventdata, handles)
handles.SongDisplay.String = handles.DList.Value;

Sign in to comment.

Answers (1)

Shubham
Shubham on 23 May 2024
Hi Tan Kye,
Your MATLAB GUI code outlines the structure for a graphical user interface but is missing the specific implementation details for linking two list boxes and a push button to display the selected string in a graph or another display element. To achieve this functionality, you will need to update the callback functions for the list boxes and the push button. Below, I will provide you with an example of how you can modify your code to link the list boxes and the push button for displaying the selected string.
Assuming you have two list boxes named CList and DList for the two different strings, and a push button named Display, you can follow this approach:
  1. Update the Callbacks for CList and DList: Modify these callbacks to store the selected string from either list box in the handles structure for later use.
  2. Implement the Display Button Callback: Use this callback to retrieve the stored string and then display it. Assuming you want to display the string in a text box or directly on a graph, you will need an appropriate display element such as a text box named SongDisplay or use the text function to display it on a graph.
Here's how you can implement these steps:
Step 1: Update the Callbacks for CList and DList
For both CList_Callback and DList_Callback, you can use the following pattern:
function CList_Callback(hObject, eventdata, handles)
% Retrieve the selected string
contents = cellstr(get(hObject,'String'));
selectedString = contents{get(hObject,'Value')};
% Store the selected string in handles structure
handles.selectedString = selectedString;
% Update the handles structure
guidata(hObject, handles);
Make sure to replicate this code for the DList_Callback as well, adjusting the function name accordingly.
Step 2: Implement the Display Button Callback
In the Display_Callback function, you will retrieve the stored string and then display it. Here's a basic example that assumes you are displaying the selected string in a text box named SongDisplay:
function Display_Callback(hObject, eventdata, handles)
% Check if the selectedString field exists in handles
if isfield(handles, 'selectedString')
% Retrieve the selected string
selectedString = handles.selectedString;
% Display the string in SongDisplay textbox
set(handles.SongDisplay, 'String', selectedString);
else
% If no string is selected, display a default message or clear the display
set(handles.SongDisplay, 'String', 'Please select a string from the list.');
end
Important Points:
  • Ensure that you have a text box or another GUI component with the tag SongDisplay to display the selected string.
  • Remember to initialize the handles.selectedString in your GUI_OpeningFcn if necessary to avoid uninitialized variable errors.
  • Adjust the visibility and enable properties of your GUI components as needed to fit your application's flow.
This approach allows you to link two list boxes and a push button to display a selected string from either of the list boxes. Adjust and expand upon this code as needed to fit your specific requirements and GUI design.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!