Loop in GUI for many trials - matlab

Initially my prog is like that (playing sound and judge what kind of sound is) :
n_repetition=10
for i=1:n_repetition
playsound(strcat(i,'wav'));
answer=input(' answer q/z/e ?','s');
switch answer
case 'q'
asw="bird";
case 'z'
asw="water";
case 'e'
asw="wind";
otherwise
disp('error');
end
end
Now I'm trying to make it more interactive with GUI, I'm using GUIDE and i've generate a .fig which contain 4 buttons: OK button, BIRD, WATER, WIND
I've also my callbacks which are empty now
What I want to do is:
-Initially all buttons are inactive
-Participant should press on ok to begin
-play sounds
-activate the buttons (sound bird, water, wind)
-catch response
-deactivate button
-wait for press ok for new trial
How could I adapt my initial code the the callback, where should I put my loop ? Thanks

Add these at the start of guiname__OpeningFcn -
handles.song_count = 0;
handles.asw = cell(10,1);
Edit your button callbacks to these -
% --- Executes on button press in ok_button.
function ok_button_Callback(hObject, eventdata, handles)
handles.song_count = handles.song_count +1;
filename = strcat(num2str(handles.song_count),'.wav');
[y,~] = audioread(filename);
%%// Use soundsc or your custom playsound function to play the sounds
soundsc(y); %playsound(strcat(i,'wav'));
guidata(hObject, handles); %%// Save handles data
return;
% --- Executes on button press in bird_button.
function bird_button_Callback(hObject, eventdata, handles)
asw = 'bird'; %%// Do something with 'asw'
handles.asw(handles.song_count) = {asw}; %%// Store the 'asw' values as a cell array
guidata(hObject, handles); %%// Save the handles data
return;
% --- Executes on button press in water_button.
function water_button_Callback(hObject, eventdata, handles)
asw = 'water'; %%// Do something with 'asw'
handles.asw(handles.song_count) = {asw}; %%// Store the 'asw' values as a cell array
guidata(hObject, handles); %%// Save the handles data
return;
% --- Executes on button press in wind_button.
function wind_button_Callback(hObject, eventdata, handles)
asw = 'wind'; %%// Do something with 'asw'
handles.asw(handles.song_count) = {asw}; %%// Store the 'asw' values as a cell array
guidata(hObject, handles); %%// Save the handles data
return;
Note: At any point of time view handles.asw to look at the button clicks history.
Suggestion: If it's okay to show the choices made by the GUI user as a list, you might consider adding a Table into the GUI. You can put the data from handles.asw into such a Table.

Related

How to call another button's callback from a button created by code

Suppose I have two pushbuttons on MATLAB GUI. When I press on pushMain, I want to call the callback of pushChild button.
I might have put simply pushChild_Callback(hObjects, eventdata, handles) in the pushMain's callback but my problem is I created pushChild object by code so there is no seperate callback function on the code.
handles.pushChild = uicontrol('Style','pushbutton');
handles.pushChild.Callback = {#printA, 1}
% this means when I press on pushChild button, it will print "A = 1"
function printA(src, event, j)
handles = guidata(src);
fprintf('A = %d\n',j);
Now when I pressed on pushMain, I want to see "A = 1" on the screen.
I tried:
function pushMain(hObject, eventdata, handles)
fprintf('1\n');
fprintf('2\n');
fprintf('3\n');
handles.pushChild.Callback(); % option 1 -> no error but nothing happens.
handles.pushChild.Callback(hObject, eventdata, handles) % option 2 -> nothing happens
Regards.

BytesAvailableFcn callback not receiving updated handles

I created an interface which automatically reads in data through the serial port, hence the reason I implemented the BytesAvailableFcn Callback
handles.fileID.BytesAvailableFcnMode = 'terminator';
handles.fileID.BytesAvailableFcn = {#streamData_fastTrak, handles};
The data that is read is displayed in a table chosen by the user (through use of radio buttons in the GUI). When an option is chosen a callback occurs to save the selected radio button to a variable which is saved in the handles struct. I have followed the program step for step and I am sure this callback does occur and that the variable is saved. However when the serial callback occurs the handles struct still has the old option value.
Here is the serial callback code:
function handles = streamData_fastTrak(hObject, eventdata, handles)
handles.num = handles.num + 1;
%receive data through serial
line = transpose(fscanf(handles.fileID, ' %f ' ));
table_data = get(handles.uitable1, 'data');
table_data_style = get(handles.uitable4, 'data');
display(handles.butt_state);
display(handles.num);
if(fix(line(1)) == 1 && strcmp(handles.butt_state, 'style_button'))
table_data_style(handles.select_Indices(1), 2:(length(line)+1)) = num2cell(line);
set(handles.uitable4, 'data', table_data_style);
display(handles.select_Indices);
elseif(fix(line(1)) > 1 && strcmp(handles.butt_state, 'stat_button'))
table_data(line(1)-1, 1:length(line)) = num2cell(line);
set(handles.uitable1, 'data', table_data);
if(line(1) == countStates(handles))
streamSensor_1_2_3(hObject, handles);
handles.time_step = handles.time_step + 1;
end
end
And the radio button callback:
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
handles.butt_state = get(get(handles.uipanel2,'SelectedObject'), 'tag');
display(handles.butt_state);
guidata(hObject, handles);
The way I see it there are 2 ways to approach the problem:
The first way (I don't recommend this as much as the second one) is to pass the data you want updates to a string control and have it read back by your serial port function.
The other way that i recommend is to include a dummy button with a call back that calls
handles.fileID.BytesAvailableFcn = {#streamData_fastTrak, handles};
Again - this will update the new "handles" data to the callback function
For example
Assuming a dummy push button with tag PB1
function handles = streamData_fastTrak(hObject, eventdata, handles)
%% do stuff here
%% update handles data
PB1_Callback (handles.PB1,event,dat)
guidata(handles.PB1,handles) %% function ends
%% dummy button callback function%%
function PB1_Callback(hObject,event,handles)
handles.fileID.BytesAvailableFcn = {#streamData_fastTrak, handles};
guidata(hObject,handles) %% dummy button function ends
You can make the dummy button invisible by making the background color of the button same as that of the UI.
When you first declare your callback function for the ByteAvailableFcn in the line:
handles.fileID.BytesAvailableFcn = {#streamData_fastTrak, handles};
Matlab assign the function handle to the event and also pass the handles stucture at this point of time. This is now frozen into the private workspace of the callback. If you change the handles structure later on in your code (as you do when you try to attach the variable handles.butt_state), the callback doesn't know it, it still use the handles structure that was passed when you declared the callback.
There are several ways of getting this value correctly but I'll give 2 of them:
1) get the value from the radio button when needed
in your streamData_fastTrak callback function, query the button state directly from the uicontrol (instead of checking for a saved value)
handles.butt_state = get(get(handles.uipanel2,'SelectedObject'), 'tag');
This way you are sure to get the latest state of the radio button.
2) Store value in appdata
Every time the button state is changed, store the value somewhere, but you still have to query this value when your callback want to execute. A good place to save values are in the appdata (accessed using setappdata and getappdata).
So in your button callback:
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
butt_state = get(get(handles.uipanel2,'SelectedObject'), 'tag');
setappdata(handles.uipanel2, 'butt_state' , butt_state );
display(butt_state);
And in your streamData_fastTrak callback:
function handles = streamData_fastTrak(hObject, eventdata, handles)
butt_state = getappdata(handles.uipanel2, 'butt_state' );
%// and the rest of your code ...

Matlab GUI: Reading from Serial port and showing in different listboxes at the same time

I am working with serial port and multiple list boxes to read data in serial port. The listboxes are used to read specific data (suppose the data is coming from 3 different sources, so I have 3 listboxes).
Now the problem:
I want to read all the data separately and continuously. I mean, when I am clicking one listbox (e.g. listbox#2) after reading listbox#1's data, the listbox#1 automatically stops, so again I need to click listbox#1 to read the data for listbox#1. But I want to get the data continuously in all the listboxes without clicking (clicking for first time to initializing) every time. How can I achieve this? I have tried other ways but not working.
Additional comment: It worked as per the below comments.
Sample code [Edited and OpeningFcn has been added]:
function main_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {#listbox1_Callback, handles});
guidata(hObject, handles);
function listbox1_Callback(hObject, eventdata, handles)
%serConn is the connection to COM port
out = fscanf(handles.serConn);
string = 'degrees1';
t=1;
while(t < 15)
if strncmp(out,string,8)
rxtext = out(14:15);
currList = get(handles.listbox, 'String');
set(handles.listbox,'String',...
[currList ; [temp]]);
end
pause(5);
t = t+1;
end

Block a KeyPressFcn while waiting mouse action

My program is a question/answer task:
-participant must press on space (keyboard) bar to play a sound
-participant must after that press on one of the two button (mouse)
-participant must press on space bar to play a sound
...
The problem is, I want to allow only one press on space bar, because user can press many time on space and play the sound x times.
How to block the figure1_KeyPressFcn while waiting the mouse response, and once we have the mouse response we reactivate the function ?
function figure1_KeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'space'
%% processing x task
%playing sounds 1000 ms
soundsc(y,Fs);
guidata(hObject, handles); %%// Save handles data
otherwise
disp('error');
end
end
function pushbutton1_Callback(hObject, eventdata, handles)
%processing task
guidata(hObject, handles); %%// Save the handles data
end
function pushbutton2_Callback(hObject, eventdata, handles)
%processing task
guidata(hObject, handles); %%// Save the handles data
end
You could use a global flag, something like this:
global clicked;
clicked = true;
Then, on your figure1_KeyPressFcn function, you only call the switch if the user has clicked, like this:
global clicked;
if clicked
switch eventdata.Key
case 'space'
clicked = false;
%% processing x task
...
end
end
And on both your pushbutton?_Callback's, you add this:
global clicked;
clicked = true;
To set clicked to true and allow figure1_KeyPressFcn to process space keys again.

imwrite current image on axes in MATLAB GUIDE

I have a problem in my GUI. In my opening function I defined an img_new variable to be an image I have stored.
My GUI has two axes, one displays the original image and the other one the filtered one. I have 4 filters in a panel with the 4 radiobuttons. And in the end of each one's code there's the img_new = the image created through the radiobutton filter.
Here's some code:
% --- Executes when selected object is changed in uipanel3.
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
handles.count = handles.count + 1;
% Change filter orientation depending on which radiobutton is chosen
switch get(eventdata.NewValue,'Tag')
case 'hte'
h_te = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(h_te);
handles.img_new = h_te;
case 'hc'
h_c = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(h_c);
handles.img_new = h_c;
case 'vlr'
v_lr = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(v_lr);
handles.img_new = v_lr;
case 'vc'
v_c = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(v_c);
handles.img_new = v_c;
end
guidata(hObject, handles)
and here's the imwrite function:
% --------------------------------------------------------------------
function save_img_ClickedCallback(hObject, ~, handles)
% writing the new image
imwrite(handles.img_new, strcat('filtered_image_', num2str(handles.count), '.png'));
guidata(hObject, handles)
Here's the function to get the image to axes1 the original) and filter it to axes2 (filtered)
% --- Executes on button press in img2.
function img2_Callback(hObject, ~, handles)
% Read image 2
img = imread('./coimbra_estadio.jpg');
handles.img_d = im2double(img);
% image size
size_img = size(handles.img_d);
handles.colums = size_img(2);
handles.rows = size_img(1);
if rem(handles.rows,2) == 0
handles.row_0 = ((handles.rows/2)+1);
else
handles.row_0 = ((handles.rows/2)+0.5);
end
if rem(handles.colums,2) == 0
handles.colum_0 = ((handles.colums/2)+1);
else
handles.colum_0 = ((handles.colums/2)+0.5);
end
axes(handles.axes1);
imshow(img);
% Generate eventdata to call the radiobuttons function
eventdata_new.EventName = 'SelectionChanged';
eventdata_new.OldValue = get(handles.uipanel3,'SelectedObject');
eventdata_new.NewValue = get(handles.uipanel3,'SelectedObject');
uipanel3_SelectionChangeFcn(handles.uipanel3, eventdata_new, handles);
guidata(hObject, handles)
As you can see, in the end I call the panel function so when the image is loaded it is automatically filtered and the axes2 image changes.
The problem is when I call the save function it saves the old img_new.
If I change the radiobutton the img_new is refreshed, but if it is not changed it is not refreshed. It should be as loading an image automatically calls for the panel of radiobuttons function.
The problem is that guidata(hObject,handles); at the end of img2_Callback saves old handles object as a final gui state, updates made in uipanel3_SelectionChangeFcn are lost. You need to eitehr manually update handles after calling uipannel3_SelectionChangeFcn by putting handles = guidata(hObject,handles);
or handles=guidata(hObject);
(forgot which call to guidata updates handles, please see help for that), or simply remove the line guidata(hObject,handles); at the end of img2_callback (less safe if code is going to change later, updating handles after uipannel3_SelectionChangeFcn is safer approach...