How to use 2 datacursors to dynamically change audio files in GUI's?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
In my GUI, I have loaded an audio file ( .wav ) into the variable storedStructure, and saved it in the handles structure - handles.str1
Now, i would like to use 2 data cursors to dynamically alter the audio playing length. For example, if the length of the audio is 5 seconds, if I were to choose 2 cursors at 2 seconds and 4 seconds, it would play only that part of the audio.
I need to put this code right away in my .m file for my GUI.
Accepted Answer
Walter Roberson
on 31 Dec 2013
Edited: Walter Roberson
on 31 Dec 2013
0 votes
Is there a reason why you need to use data cursors instead of (for example) ginput() ?
What is the data cursor to be applied against? Data cursors require a graph, but you have not indicated anything as being graphed. Are you graphing the signal level, or are you graphing the time?
Why not use a couple of sliders?
13 Comments
Mayank Amencherla
on 31 Dec 2013
Well, the person who gave me this assignment asked me for the ability to pinpoint any 2 points on the graph, and use those points to play.
I understand I can use sliders, but that would not let me use 2 points, but only 1 starting point.
Correct me if I'm wrong?
Walter Roberson
on 31 Dec 2013
You can use two sliders. Or you could use ginput(2) to collect the two points.
Mayank Amencherla
on 31 Dec 2013
Edited: Mayank Amencherla
on 31 Dec 2013
a = get(handles.slider1, 'Value');
x = 0:16000;
y=a*x;
sig = audioread(handles.str1);
plot(handles.Audio,y,sig);
This is the code I thought I should use. It is clearly wrong. Do you know how I can change this to fit my needs?
Walter Roberson
on 31 Dec 2013
What do you intend your "a" value to represent?
Mayank Amencherla
on 1 Jan 2014
Okay. So basically, I would like the slider to represent the audio file time. Like, as I move through the file, I want to play the file from that part, and simultaneously plot the graph from that point to the end.
For example. I use audio read (handles.str1) and I want to plot and play the file dynamically as I change the slider.
Hope that makes sense.
Walter Roberson
on 1 Jan 2014
[sig, fs] - audiread(handles.str1);
nsamp = size(sig,1);
slider_value = get(handles.slider1, 'Value'); %assume slider is configured for percentage
first_samp = 1 + floor(slider_value / 100 * nsamp);
plot(handles.Audio, first_samp : nsamp, sig(nsamp:end, :));
audioplayer( sig(nsamp:end, :), fs)
Mayank Amencherla
on 1 Jan 2014
Edited: Mayank Amencherla
on 1 Jan 2014
Hey, the wave isn't plotting. I used the same code. However, there is no error that is associated with the code.
Walter Roberson
on 1 Jan 2014
You might want to add
drawnow()
after the plot()
Also exactly how did you configure your slider? The above assumes it was configured for minimum 0 and maximum 100 (percent)
Mayank Amencherla
on 1 Jan 2014
Edited: Mayank Amencherla
on 1 Jan 2014

I did configure it to percentage.
Walter Roberson
on 1 Jan 2014
If you are going to put it into your slider callback, replace handles.slider1 with hObject
However, I would not put it in the slider callback. The slider callback often gets called multiple times while you are moving it longer distances or fine-tuning it. You should adjust the position and then have something else to trigger starting the playing and plotting.
The error message is indicating that you have either deleted the slider that handles.slider1 referred to, or you never initialized it to a valid slider handle. You might have deleted the slider if your code creates the slider with an explicit uicontrol() call, rather than having the slider created and stored with the .fig .
Note for clarity: once you move the action outside the slider callback, you would not use hObject to refer to the slider; instead you would track down why handles.slider1 was not correct and fix that problem so that handles.slider1 can be used in the place you initiate the plotting and playing.
If you need to, you could use
handles.slider1 = findall(0, 'Tag', 'slider1');
Mayank Amencherla
on 1 Jan 2014
Okay, I understood that. Here are 2 pictures of the 2 callbacks in question. What am I doing wrong here?
<<

>>
Mayank Amencherla
on 1 Jan 2014
Edited: Mayank Amencherla
on 1 Jan 2014

There are no errors but the Audio axes aren't plotting the graph....
Walter Roberson
on 1 Jan 2014
Correction:
plot(handles.Audio, first_samp : nsamp, sig(first_samp:end, :));
More Answers (0)
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Products
See Also
on 31 Dec 2013
on 1 Jan 2014
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)