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

1 vue (au cours des 30 derniers jours)
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;
set(handles.text3,'String',num2str(a));
disp(x);
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?
  2 commentaires
Jan
Jan le 3 Mai 2021
I do not undestand, what you are asking for.
What is the purpose of:
x = (1:1);
Teo Say Yee
Teo Say Yee le 5 Mai 2021
Modifié(e) : Teo Say Yee le 5 Mai 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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 3 Mai 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]);
end
  8 commentaires
Teo Say Yee
Teo Say Yee le 7 Mai 2021
@Jan Thank you for replying me still. I will try to explain using some of my real programme to show you what im trying to do.
Basically, I want the user to create their own music and add the selected notes into a variable at 'Line 1' and to be played. I am struggling to create a programme for this coding part. I am able to set strings to 'Line 1' like what you have shown me, but i couldn't play those notes in 'Line 1' with sound function because strings are not variable as we know. So is there a way to connect strings and variables which both have the same name ? or is there other way to do the programme ?
Jan
Jan le 7 Mai 2021
Modifié(e) : Jan le 7 Mai 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;
...
end
sound(sin(freq * 2 * pi * (0:1/8192:.4)));
end
% See: https://pages.mtu.edu/~suits/notefreqs.html
See also:

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by