How to display image information using a pushbutton in GUI ?

CODE :
function Display_Information_Callback(hObject, eventdata, handles)
% hObject handle to Display_Information (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename pathname]=uigetfile({' *.jpg';'*.png'},'File Select');
image=strcat(pathname,filename);
[pathstr,name ,ext ,versn]=fileparts(filename);
fileinfo=imfinfo(image);
FileSize1=fileinfo.FileSize(1,1);
sizew=fileinfo.Width(1,1);
sizeh=fileinfo.Height(1,1);
axes(handles.axes2)
imshow(image)
set(handles.edit5,'string',name);
set(handles.edit7,'string',sizew);
set(handles.edit8,'string',sizeh);
set(handles.edit6,'string',FileSize1);
set(handles.edit9,'string',ext);
set(handles.edit11,'string',image);
GUI Designed :
ERROR :
Error using
fileparts
Too many
output
arguments.
Error in
userspecified>Display_Information_Callback
(line 160)
[pathstr,name
,ext
,versn]=fileparts(filename);
Error in
gui_mainfcn
(line 95)
feval(varargin{:});
Error in
userspecified
(line 42)
gui_mainfcn(gui_State,
varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)userspecified('Display_Information_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 1 Aug. 2021
fileparts() does not report version number. Try this (with a number of other imnprovements):
[filename, pathname] = uigetfile({' *.jpg';'*.png'},'File Select');
fullFileName = fullfile(pathname,filename);
[folder, baseFileNameNoExt, ext] = fileparts(fullfileName);
fileinfo = imfinfo(fullFileName);
FileSize1 = fileinfo.FileSize(1,1);
axes(handles.axes2)
theImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(theImage);
imshow(theImage);
handles.edit5.String = baseFileNameNoExt;
handles.edit7.String = columns;
handles.edit8.String = rows;
handles.edit6.String = FileSize1;
handles.edit9.String = ext;
handles.edit11.String = fullFileName;

4 Kommentare

If i want to display information of an image that is already being displayed in gui and as i click "display information" button i donot have to select the image again , how can i achieve it? I have changed the first line , is it correct?
a = getappdata(0,'a',[pathname,filename]);
fullFileName = fullfile(pathname,filename);
[folder, baseFileNameNoExt, ext] = fileparts(fullFileName);
fileinfo = imfinfo(fullFileName);
FileSize1 = fileinfo.FileSize(1,1);
axes(handles.axes2)
theImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(theImage);
imshow(theImage);
handles.edit5.String = baseFileNameNoExt;
handles.edit7.String = columns;
handles.edit8.String = rows;
handles.edit6.String = FileSize1;
handles.edit9.String = ext;
handles.edit11.String = fullFileName;
ERRORS :
Undefined function or
variable 'pathname'.
Error in
userspecified>Display_Information_Callback
(line 159)
a =
getappdata(0,'a',[pathname,filename]);
Error in gui_mainfcn (line
95)
feval(varargin{:});
Error in userspecified (line
42)
gui_mainfcn(gui_State,
varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)userspecified('Display_Information_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
pathname was never added to appdata, so you can't get it out. Why don't you just attach it as a field of handles when you first get the file name in Display_Information_Callback()? That's probably the simplest way.
handles.fullFileName = fullFileName;
I could'nt understand.Kindly see the GUI below. For example, i have browsed this image, when i shall click 'display information', it should only display the information ,without selecting the image again.That's what i want to do.
Have two functions, each one a callback for the corresponding button. One button to read in the image and display it's information, and the other button to simply display the image but not read it in again with imread().
%=====================================================================
% Button callback function to read in image and display information.
function Read_Info_and_Display_Information_Callback(hObject, eventdata, handles)
% Read in image and display info.
handles = Read_Info_and_Display_Information(handles, true);
% Update handles structure
guidata(hObject, handles);
return; % from Read_Info_and_Display_Information_Callback()
%=====================================================================
% Button callback function to display existing information only.
function Display_Information_Callback(hObject, eventdata, handles)
% Don't read in image (assume it's been done already).
% Just display information again.
handles = Read_Info_and_Display_Information(handles, false);
% Update handles structure
guidata(hObject, handles);
return; % from Display_Information_Callback()
%=====================================================================
% Function to optionally read in image,
% and to display information about the image.
function handles = Read_Info_and_Display_Information(handles, readFromDisk)
if readFromDisk
[filename pathname]=uigetfile({' *.jpg';'*.png'},'File Select');
[filename, pathname] = uigetfile({' *.jpg';'*.png'},'File Select');
fullFileName = fullfile(pathname,filename);
handles.fullFileName = fullFileName;
end
fullfileName = handles.fullfileName;
[folder, baseFileNameNoExt, ext] = fileparts(fullfileName);
fileinfo = imfinfo(fullFileName);
FileSize1 = fileinfo.FileSize(1,1);
axes(handles.axes2)
theImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(theImage);
imshow(theImage);
handles.edit5.String = baseFileNameNoExt;
handles.edit7.String = columns;
handles.edit8.String = rows;
handles.edit6.String = FileSize1;
handles.edit9.String = ext;
handles.edit11.String = fullFileName;
return; % from Read_Info_and_Display_Information()

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu System Commands finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by