How to Display Only Filename and FileSize in MATLAB
-
21-12-2019 - |
Question
when i try to open an image using axes as button its display filename with source path.
- File Name : C:\Users\Win8\Documents\MATLAB\Test\lena.png
- File Size : 606.09
i just want to display filename (red border) only and filesize in KB (blue border) without decimal.
- File Name : lena.png
- File Size : 606
axes1 Callback Code:
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
project = guidata(gcbo);
[imgname, imgpath] = uigetfile({'*.png';}, 'Open an Image')
if imgname==0 % if it is canceled
imgname=''; % create an empty name
imgpath=''; % create an empty path
end
if isequal(imgname, 0)
return;
end
eval(['cd ''' imgpath ''';']);
I=imread(fullfile(imgpath, imgname));
set(project.figure1, 'CurrentAxes', project.axes1);
set(imshow(I));
imshow(I);
set(project.figure1, 'Userdata', I);
set(project.axes1, 'Userdata', I);
info = imfinfo(fullfile(imgpath, imgname));
set(project.edit1, 'String', info.Filename);
set(project.edit2, 'String', info.FileSize/1024);
any advice how to do it??
i'm using MATLAB R2012a.
Solution
For the first part: use fileparts
:
[~, fn, ext] = fileparts(info.Filename); %// get filename (no path) and extension
set(project.edit1, 'String', [fn ext])
For the second part: use round
:
set(project.edit2, 'String', round(info.FileSize/1024))
or maybe explicitly indicate units (kB) in the string:
set(project.edit2, 'String', [num2str(round(info.FileSize/1024)) ' kB'])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow