Update Callback status in MATLAB GUI - matlab

I have developed a GUI interface in MATLAB. When I push a button search, I have seen the desirable result. However, when I change the textbox and push the search button again, it does not work and gives me following error:
Undefined function 'untitled2' for input arguments of type 'struct'.
Error in #(hObject,eventdata)untitled2('edit1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Undefined function 'untitled2' for input arguments of type 'struct'.
Error in #(hObject,eventdata)untitled2('pushbutton16_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I must re-execute the all code! Is any way to repeatedly run the GUI?
As seen, when I change the Video ID to other number and push the search button, the results are not updated.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%pathname='C:\Users\Dr Syed Abdul Rahman\Desktop\innovation final\video detail\';
string1 = get(handles.edit1,'UserData');
fName=strcat(cd,'\Video Detail\Video Detail',string1);
fid = fopen(fName);
if fid~=-1
s{1} = fgetl(fid);
s{2} = fgetl(fid);
s{3} = fgetl(fid);
s{4} = fgetl(fid);
s{5} = fgetl(fid);
s{6} = fgetl(fid);
s{7} = fgetl(fid);
set(handles.text4,'Visible','On');
set(handles.edit1,'Visible','On','String',s{1})
set(handles.edit2,'Visible','On','String',s{2})
set(handles.edit3,'Visible','On','String',s{3})
set(handles.edit4,'Visible','On','String',s{4})
set(handles.edit5,'Visible','On','String',s{5})
set(handles.edit6,'Visible','On','String',s{6})
set(handles.edit7,'Visible','On','String',s{7})
set(handles.axes4,'Visible','On');
cd './Images';
A = imread(s{1});
axes(handles.axes4)
imshow(A);
else
set(handles.text3,'Visible','On','String','File is not exist !')
end

Instead of this line "string1 = get(handles.edit1,'UserData');"
try this one
string1 = get(handles.edit1,'String');

There are a lot of weird things going on in pushbutton16_Callback:
You don't need to keep setting 'Visible','on' for all your editboxes
get 'String' like amir nemat said, not 'UserData'
Use fullfile instead of strcat
Don't forget fclose(fid)!
Don't do cd './Images' in a callback unless you cd back but even then it's not a good idea, just imread into that path.
Do imshow(A,'Parent',handles.axes4) instead of axes(handles.axes4); imshow(A);
Also, you might want to rename your GUI to something other than untitled2. ;)
As for why you are getting the error, I don't know for sure, but I suspect when gui_mainfcn tries to feval your untitled2.m to run the callback, it is running something else. Check for other untitled2 MATLAB-executable files: which -all untitled2.

You problem can come that you change your working folder when you use :
cd './Images';
A possible correction could be :
oldPath = cd('./Images'); % Return the path that you were before
A = imread(s{1});
axes(handles.axes4)
imshow(A);
cd(oldPath); % Go back in the folder with all your functions

Related

Error: Missing multiplication operator, missing or unbalanced delimiters. To construct matrices, use brackets instead of parentheses

am trying to design an interface using the MATLAB APP DESIGNER. However, I am getting an error message.
This is for creating a user interface in using MATLAB APP DESIGNER
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, varargin)
% Configure image axes
app.ImageAxes2.Visible = 'off';
app.ImageAxes.Colormap = gray(256);
axis(app.ImageAxes, 'image');
app.ImageAxes3.Visible = 'off';
app.ImageAxes.Colormap = gray(256);
axis(app.ImageAxes, 'image');
end
% Callback function
function DropDownValueChanged(app, event)
% Update the image and histograms
updateimage(app, app.DropDown.Value);
end
% Callback function
function LoadButtonPushed(app, event)
% Display uigetfile dialog
filterspec = {'*.jpg;*.tif;*.png;*.gif','All Image Files'};
[f, p] = uigetfile(filterspec);
% Make sure user didn't cancel uigetfile dialog
if (ischar(p))
fname = [p f];
updateimage(app, fname);
end
end
I expected the user interface to be displayed after running the simulation. But I am getting the following error message:
"Missing multiplication operator, missing or unbalanced delimiters. To construct matrices, use brackets instead of parentheses".
The errors point to the line that uses the METHODS function. However, that particular line in MATLAB APP DESIGNER code view is gray in color. As a result, I am unable to edit that particular line. Any suggestion would be appreciated.

Loading multiple images in Matlab GUI

I want to load multiple images in Matlab GUI.
Algorithm below:
% --- 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)
[filename path] = uigetfile('*.jpg','*.png','Chose files to
load','MultiSelect','on');
if isequal(filename,0) || isequal(path,0)
return
end
if iscell(filename)
img = cell(size(filename));
for ii = 1:numel(filename)
img{ii} = imread(fullfile(path,filename{ii}));
end
else
img{1} = imread(fullfile(path,filename));
end
filename = strcat(path,filename);
fullpathname = strcat(path, filename);
set(handles.edit1,'String', fullpathname);
fileID = fopen(strcat(path, filename), 'r');
It works in case of loading one image, but in case of loading multiple images, it gives mi subsequent error:
Error using imread>parse_inputs (line 457)
The file name or URL argument must be a string.
Error in imread (line 316)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in untitled>pushbutton1_Callback (line 112)
im = rgb2gray(imread(filename));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)
untitled('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Could you please give me a hint, so I could make it functional?
uigetfile returns in filename:
a character vector or a cell array of character vectors.
(From the documentation). The former happens when selecting one file, the latter when selecting multiple files.
Thus, if you want to be ale to select multiple files, your code needs to handle that case by checking to see if iscell(filename), and if so, looping over each of its elements.
Also, please use fullfile to concatenate parts of a path or file name, it will prevent portability issues down the road.
You could write code like this:
[filename,path] = uigetfile({'*.jpg';'*.png'},'Chose files to load','MultiSelect','on');
if isequal(filename,0)
return
end
if iscell(filename)
img = cell(size(filename));
for ii = 1:numel(filename)
img{ii} = imread(fullfile(path,filename{ii}));
end
else
img{1} = imread(fullfile(path,filename));
end
Now img is a cell array containing all the images selected.

Growing a struct with GUI

I've been working on a little custom made database in MATLAB.
I have a GUI with a bunch of 'Edit Text' boxes and buttons.
The key is that I should be able to register an undefined number of students with some information like names, surnames, code etc. I've managed to store only one student (i.e the first time i push the 'Submit Button') but when i enter another student's information, MATLAB just overwrites the information from the previous registration.
Here's the Callback for the 'Submit' button
function Submit_Callback(hObject, eventdata, handles)
global n
n=n+1
% hObject handle to Submit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
name1 = get(handles.name,'String'); %edit1 being Tag of ur edit box
name2=get(handles.name2,'String');
name3=get(handles.name3,'String');
major=get(handles.major,'String');
labavg=num2str(get(handles.labavg,'String'));
finalgrade=num2str(get(handles.finalgrade,'String'));
email=num2str(get(handles.email,'String'));
code=num2str(get(handles.code,'String'));
for ii=1:numel(n)
student_information(ii).name=name1
student_information(ii).surname1=name2
student_information(ii).surname2=name3
student_information(ii).code=code
student_information(ii).major=major
student_information(ii).final_grade=finalgrade
student_information(ii).laboratory_average=labavg
student_information(ii).email=email
end
assignin('base', 'student_information', student_information)
end
I've declared the counter 'n' as a global variable set to '0' in the workspace.
I'm not sure if my code isn't looping properly. Maybe the mistake is in there but I can't see how to fix it.
Can you please help me with my code?
Thanks!
I'm not sure what you were trying to achieve with your loop, but I don't see the need for it. Also, by using assignin, you are overriding the contents of student_information in your workspace. You are better off making student_information global in Submit_Callback in addition to n, then construct a new_student structure using your information and append it to student_information as follows:
name1 = get(handles.name,'String'); %edit1 being Tag of ur edit box
name2=get(handles.name2,'String');
name3=get(handles.name3,'String');
major=get(handles.major,'String');
labavg=num2str(get(handles.labavg,'String'));
finalgrade=num2str(get(handles.finalgrade,'String'));
email=num2str(get(handles.email,'String'));
code=num2str(get(handles.code,'String'));
new_student.name = name1;
new_student.surname1 = name2;
new_student.surname2 = name3;
new_student.major = major;
new_student.laboratory_average = labavg;
new_student.final_grade = finalgrade;
new_student.email = email;
new_student.code = code;
student_information(n) = new_student;
n = n + 1;
and that should append the new entry at the end of the struct array.

handles are getting deleted

I have 3 row-vectors, and would like to output them out of my GUI, if I close it.
First I tried it with global variables in the GUI, which i access and put them in the output function:
global horizontalFrames;
global verticalFrames;
global blackFrames;
varargout{1} = horizontalFrames;
varargout{2} = verticalFrames;
varargout{3} = blackFrames;
But, all values are ALREADY AT THIS POINT zero, even if i set them in the code.
Why are they set zero?
Then I tried it by the use of handles like this:
handles.horizontalFrames = horizontalFrames;
handles.verticalFrames = verticalFrames;
handles.blackFrames = blackFrames;
somewhere in the code (where the values are NOT all zero)
and then write this in the output function:
varargout{1} = handles.horizontalFrames
varargout{2} = handles.verticalFrames
varargout{3} = handles.blackFrames
the handles cant be found. It seems, like the handles are not accessible from the output function, or they are deleted. I'm desperate... where is my data, why can't I simply output it?
I found it out. I had to update my handles with
guidata(hObject, handles);
"somewhere in the code" again, means, immediately after adding my variables.
Greets, hope it helps someone.

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.