Matlab gui error - matlab

this is a part of my gui which executes when i click on the push button
//
//
//
%VERIFICATION
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
DIR=handles.directory;
%angle=Angles(DIR);
area=nor_area(DIR);
%area=0.002;
%display(area)
Check=verify(area);
%display(Check);
if(Check==0)
%message = sprintf('nClick the OK button to continue');
msgbox('The signature belongs to the same person!!!');
else
msgbox('The signature is forged!!!');
end
//
//
//
and this is the verify function
//
//
//
function flag= verify(area)
%area=0.8969;
%take=area;
%display(take)
flag=0;
extract = xlsread('D:\Project\Image_processing\important\best.xlsx', 'CW4:CW17');
c=size(extract);
%display(c)
for k = 1:c
if (extract(k)==area)
display(extract(k));
flag=1;
end
end
%display(flag)
//
//
//
best.xlsx is the excel file from which I am retrieving the values and comparing with the value I get from the main gui function. The problem is even if I send the area value explicitly its not working. If I try running each function individually that is gui separately and verify separately it sets the flag correctly, but when I run it together it's not setting the flag properly.

Take another look at the following lines in the verify function:
c = size(extract);
for k = 1:c
I presume you really want
c = numel(extract);
because size returns a vector.

Related

MATLAB GUI: updating variables to mat file

I have program where at the end a GUI is launched. I built it using guide. I load 4 variables from my program into 4 GUI text boxes using a mat file called n.mat (and a pushbutton feature).
In the program
n = [nuno, ndue, ntre, nquattro];
save n.mat
In the GUI interface pushbutton
% --- Executes on button press in upload.
function upload_Callback(hObject, eventdata, handles)
% hObject handle to upload (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
S = load('n.mat');
handles.v1 = S.nuno;
handles.v2 = S.ndue;
handles.v3 = S.ntre;
handles.v4 = S.nquattro;
set(handles.initial1,'String',num2str(handles.v1));
set(handles.initial2,'String',num2str(handles.v2));
set(handles.initial3,'String',num2str(handles.v3));
set(handles.initial4,'String',num2str(handles.v4));
guidata(hObject, handles);
Then I have other 4 text boxes where I change the value of the variables and save them in another mat file. I'm not sure if I'm doing this correctly.
In the program (before calling myGUI) I initialize the m vector for the updated variables.
nunof = 0;
nduef = 0;
ntref = 0;
nquattrof = 0;
m = [nunof, nduef, ntref, nquattrof];
save m.mat
In the program (after calling myGUI) I try and load the m.mat file and extract the variables from it so I can use them in some calculations further in the program.
load m.mat;
nunof = m.nunof;
nduef = m.nduef;
ntref = m.ntref;
nquattrof = m.nquattrof;
Before this, in the GUI interface 'done' button I try and save my inputs into the m.mat file like this:
function done_Callback(hObject, eventdata, handles)
% hObject handle to done (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% save the parameters to file
load('m.mat');
m = [nunof, nduef, ntref, nquattrof];
nunof = str2num(get(handles.final1,'String'));
nduef = str2num(get(handles.final2,'String'));
ntref = str2num(get(handles.final3,'String'));
nquattrof = str2num(get(handles.final4,'String'));
save('m.mat','-append');
I want to know why this isn't working and how can I change it. Thanks a lot.
You can't use save('m.mat','-append');. You're missing an option to get to append.
In order to use append you have to declare a filename, the variable and then append.
save(filename,variables,'-append')
Taken from - https://au.mathworks.com/help/matlab/ref/save.html
Also, from your code you're not redefining the variables in your m struct.

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 ...

can't apply image gaussian blur filter using slider in matlab

I am working in image processing task using matlab,I have made a slider inside a dialog to apply and update gaussian blur filter on an image which is shown in axes
but it can not apply it,it shows that error
Error in ==> MatlabTestProject>name at 392
axes(handles.axes4);
??? Error while evaluating uicontrol Callback
this is my code
function mygui()
out = dialog('WindowStyle', 'normal', 'Name', 'My Dialog','Resize','off');
hSlider = uicontrol('Style','slider','Min',3,'Max',15,'Value',3,'Callback',#gaussian_blur);
%hListener = addlistener(hSlider,'Value','PostSet',#(s,e) disp('hi'));
function gaussian_blur(s,e,handles)
global imag;
slider_value = get(s,'Value');
slider_value=round(slider_value);
%display(slider_value);
%disp('hello')
%create filter
%sliderValueTxt=num2str(slider_value);
%set(handles.kSizeValueText ,'String',sliderValueTxt);
h = fspecial('gaussian', slider_value,0.5);
imag=imfilter(imag,h,'conv');
axes(handles.axes4);
imshow(imag)
% --------------------------------------------------------------------
function gaussianBlur_Callback(hObject, eventdata, handles)
% hObject handle to gaussianBlur (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
gaussian_dialog_Gui();
You're actually really close! All you need to do is include handles as an argument to the function gaussian_blur when defining the slider's listener object callback.
i.e replace this line: (it looks like a test line though)
hListener = addlistener(hSlider,'Value','PostSet',#(s,e) disp('hi'));
with something like this:
hListener = addlistener(hslider,'Value','PostSet',#(s,e) gaussian_blur(handles));
Just to be sure it works I created a test GUI programmatically using an axes and a slider and it works very well! Actually I changed a bit the filter to see an effect on my test image, but it should work as well in your case:
function GaussianSlider()
clear
clc
close all
handles.Image = imread('peppers.png');
handles.fig = figure('Position',[500 500 600 600],'Units','pixels');
handles.axes1 = axes('Units','pixels','Position',[50 100 400 400]);
handles.slider = uicontrol('Style','slider','Position',[50 50 400 20],'Min',3,'Max',15,'Value',3);%// I commented this for the purpose of demonstration. 'Callback',#gaussian_blur(handles));
%// That's the important part: add 'handles' as input argument to
%// gaussian_blur.
handles.Listener = addlistener(handles.slider,'Value','PostSet',#(s,e) gaussian_blur(handles));
imshow(handles.Image,'Parent',handles.axes1);
guidata(handles.fig);
function gaussian_blur(handles)
slider_value = round(get(handles.slider,'Value'));
%// I modified a bit the filter to see the effect
h = fspecial('gaussian',slider_value,slider_value);
handles.Image=imfilter(handles.Image,h,'conv');
axes(handles.axes1);
imshow(handles.Image)
end
end
If we look at 2 screenshots (i.e. 2 different slider positions):
and after moving the slider:
That's it! Hope that helps! If something is unclear please tell me.
Oh and in case you do not know: The actual callback of a slider is only executed when you release the button or press either arrow. As long as you hold the slider and move it, it's only the listener's callback that will be executed.

Loop in GUI for many trials

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.

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...