Push the left or right button in the mouse after generating a sound
4 views (last 30 days)
Show older comments
Hello everybody, I have a problem in this following code:
here I generated some sounds randomly to left and right ear and right now I want, when I hear a sound in the left ear, I push the left button in the mouse and simultaneously the program save X=1 in the matrix and vice versa ( for a sound in the right ear, I push the right button in the mouse and save X=2 ) I can not make this code that is related to push the button left or right and it can save the information(1= left button or 2=Right button)
I am not familiar with this issue and I truly need it, Does anyone have any ideas?? (I am getting confused)
clc;
clear;
SamplingRate=48000;
F=[250 500 1000 2000 4000];
t=linspace(0,1,SamplingRate);
ind=0;
memory{1,1}={0};
for i=1:length(F)
ind=1+ind;
Frequency = F(i);
y=sin(t*2*pi*Frequency);
memory{1,ind} = y';
end
L = numel(memory);
thisdir{1}={0};
direction = {'L', 'R'};
for h=1: L
r1(h) = randi([1, 2], 1); % Get a 1 or 2 randomly.
thisdir(h) = direction(r1(h));
end
ind=0;
r2= randperm(L);
out_all=[];
for ci = 1:L
ind = ind + 1;
y=zeros(length(memory{1,r2(ci)}),2);
if strcmpi(thisdir{1,ind},'L')
y(1:length(memory{1,r2(ci)}),1)=memory{1,r2(ci)};
else
y(1:length(memory{1,r2(ci)}),2)=memory{1,r2(ci)};
end
%% here I want,when I hear a sound in the left ear, I push the left button in the mouse and simultaneously the program save X=1 in the matrix and vice versa ( for a sound in the right ear, I push the right button in the mouse and save X=2 )
sound(y,SamplingRate) ;
pause(5)
out_all = [out_all; y];
end
audiowrite('test.wav',out_all,SamplingRate);
0 Comments
Answers (1)
Geoff Hayes
on 22 Sep 2021
mohadeseh - one way (and there may be another) is to create a figure and have that figure capture the mouse button events (assuming the mouse pointer is hovering over the figure). For example, code like
function MouseButtonExample
close all;
hFig = uifigure;
set(hFig,'WindowButtonDownFcn',@ButtonDown);
hLabel = uilabel(hFig);
hLabel.Position = [0 0 hFig.Position(3) hFig.Position(4)];
hLabel.Text = 'Press the Left or Right Mouse button when you hear the sound in the corresponding ear.';
hLabel.HorizontalAlignment = 'Center';
leftButtonCount = 0;
rightButtonCount = 0;
function ButtonDown(hObject, eventdata)
mouseSelectionType = hFig.SelectionType;
if strcmp(mouseSelectionType,'normal')
leftButtonCount = leftButtonCount + 1;
elseif strcmp(mouseSelectionType,'alt')
rightButtonCount = rightButtonCount + 1;
end
fprintf('leftButtonCount = %d\n', leftButtonCount);
fprintf('rightButtonCount = %d\n', rightButtonCount);
end
end
would count the number of left button clicks and right button clicks given the Figure SelectionType property. This may be sufficient to allow you to assign the mouse button (1 or 2) to the X matrix. In order to integrate the above, you will probably want to convert your above code from a script to a function so that you can nest the callback within that main function (like how I nest ButtonDownCallback within the main MouseButtonExample function.
2 Comments
Geoff Hayes
on 27 Sep 2021
I think you need a figure though to capture the mouse click events. There might be a java solution but I'm not sure how doable that is either. You can always plot somehting on the figure (the sound wave?) to make it look like it is something more relevant than a blank figure with text.
See Also
Categories
Find more on Audio I/O and Waveform Generation 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!