MATLAB Answers

How to create a programme that will increase its increment of the size of array with a push button ? Using GUIDE

3 views (last 30 days)
Teo Say Yee
Teo Say Yee on 3 May 2021
Edited: Jan on 7 May 2021
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = (1:1);
a = get(handles.listbox1,'Value');
x(end+1)= a;
Basically, I want to create an array of variables with different values each and play them with function 'sound'. The array of variables are selected by user using variables from listbox and are added into array using 'add pushbutton'. For example I have a listbox of 'A' 'B' 'C', and when the user chose the order 'C' 'A' 'A' 'B' 'B' to be sounded. How do I create that code to put in what user selected from listbox and put them into array accordingly?
Teo Say Yee
Teo Say Yee on 5 May 2021
@Jan Hi, I wanted to create a variable for the programme like this for example : Song = [ A A B B C ], then sound(Song,fs). Each of the A B C variables have their own values (harmonic frequency values which are double data type). So when user clicked 'add button' with their selection, it will be added to the variable 'song'. its like variables in variables. I am not sure if im making sense.

Sign in to comment.

Accepted Answer

Jan on 3 May 2021
With some guessing:
function pushbutton1_Callback(hObject, eventdata, handles)
Value = get(handles.listbox1, 'Value');
Data = get(handles.listbox1, 'String');
Selected = Data{Value};
% Append selection from list box to text field:
old = get(handles.text3, 'String');
set(handles.text3, 'String', [old, Selected]);
Jan on 7 May 2021
"connect strings and variables which both have the same name" - this is far too complicated. Use the contents of the string to decide, which sound to play:
Str = '';
% Now append the notes in the callbacks - here emulated by code:
Str = [Str, 'C3 '];
Str = [Str, 'D3 '];
Str = [Str, 'C4 '];
... etc.
Finally you can "play" the notes:
Notes = strsplit(Str, ' '); % ==> {'C3', 'D3', 'C4', ...}
for k = 1:numel(Notes)
switch Notes{k}
case 'C3'
freq = 130.81;
sound(sin(freq * 2 * pi * (0:1/8192:.4)));
% See:
See also:

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!

Translated by