MATLAB Answers

How to display image information using a pushbutton in GUI ?

2 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 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 Comments
Image Analyst
Image Analyst on 1 Aug 2021
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()

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by