implement a made function and code in a gui
4 views (last 30 days)
Show older comments
Hi!
I want to implement a code including a function in a gui to visualize it. The code exists of recording sounds in real time. And I want to record sound and visualize this in the graphs when I press on a button. Is this possible?
I have this:
% --- Executes on button press in pushbutton1.
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)
handles.PsychPortsoundrecord
guidata(hObject,handles)
plot(1:nrsamples, audiodata(1,:), 'r');
0 Comments
Answers (1)
Soumya Saxena
on 12 Aug 2016
I understand that you want to associate code with the pressing of a button. In your case, you would like to record sounds and visualize the data in graphs when you press a button.
You can perform this using “GUIDE” in MATLAB. You can launch the MATLAB guide using ‘guide’ command in the command window and drag a ‘push button’ object to the interface.
As a simple example, the following GUI has an edit text box and a push button. The tag of the edit box is ‘enter’ and the tag of the push button is ‘plot’. You can change the tags of the components by right clicking the component in the guide interface and choosing the property inspector. This will open a window, where you can modify properties of the components.
You can now, associate a callback function with a push button in Guide. By right clicking on the button and going to the “view callbacks” option, you can select the appropriate call back. MATLAB will automatically generate an empty call back function body and you can add your code in it.
When you enter a value in the edit box, the call back function of the push button retrieves this value, n and generates a random matrix of dimensions n x n. It then, plots the matrix and displays the results. This way, we can read and visualize data.
The call back function for the push-button, “press” would be :
% --- Executes on button press in plot.
function plot_Callback(hObject, eventdata, handles)
% hObject handle to plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
nu = str2num(get(handles.enter, 'String'));
r=rand(nu);
plot(r);
Please also find the MATLAB files attached and the figure. The code should be run from the M file, "example.m".
For more information about call back functions, you can refer to the following documentation: http://www.mathworks.com/help/matlab/creating_guis/add-code-for-components-in-callbacks.html#f10-1001407
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!