How do I split the file path into just a file name? Matlab GUI

I'm making a checkbox that's supposed to take a filepath from a listbox created and it needs to reduce the string seen in the listbox down to just the file name. Now the more complicated side of this is that it needs to do this for multiple strings. So let's say I import 3 files into the listbox, it needs to cut down the entire directory path into just the file name. When the box is unchecked, it should be able to bring back the original full path name in the box.
Any suggestions? It's been driving me nuts

 Accepted Answer

I have no idea what "When the box is unchecked" means. Listboxes cannot be checked or unchecked. I guess you have a listbox with file names and a check box to show or hide the paths.
See doc fileparts.
File = 'C:\Your\Folder\Name.txt'
[fPath, fName, fExt] = fileparts(File);
% fPath = C:\Your\Folder
% fName = Name
% fExt = .txt
Maybe you want:
FileExt = [fName, fExt]
Please clarify, what "file name" means exactly: With or without extension?
Now you want to hide or show the file path on demand?
FileList = {'C:\Your\Folder\Name.txt', ...
'D:\Another\Folder\Name2.dum', ...
'E:\Where\Ever\Name3.hello'};
% Store this list persistently, e.g. in the UserData of the edit field:
set(handles.edit1, 'UserData', FileList);
% Alternatively:
% handles.FileList = FileList;
% guidata(hObject, handles);
You need a helper function, because fileparts does not accept cell strings yet (blame MathWorks):
function [fPath, fName, fExt] = SuperFileParts(List)
if iscellstr(List)
fPath = cell(size(List));
fName = cell(size(List));
fExt = cell(size(List));
for k = 1:numel(List)
[fPath{k}, fName{k}, fExt{k}] = fileparts(List{k});
end
elseif ischar(List)
[fPath, fName, fExt] = fileparts(List);
else
error('Input not handled.')
end
end
Now in the callback of the checkbox:
function CheckboxCallback(objectH, EventData, handles)
if objectH.Value
handles.edit1.String = handles.edit1.UserData;
else
handles.edit1.String = SuperFileParts(handles.edit1.UserData);
end
end

3 Comments

Sorry if I was unclear, "When the box is unchecked" refers to after you uncheck the checkbox. As far as the "file name" is concerned, I would like to have it with the file extension. When the button is checked, it should hide the entire file path and just show the file name with its corresponding file extension.
The goal press a button that will open up the directory, then grab the file and place it into the listbox. Just as a way to clean it up, I wanted to just include the file name and the extension. So if a user imports multiple files from any folder or drive, it should understand where to cut off the rest of the path and just include the file name and extension. Is there a way to have the function know where to do this? I'm not too sure if the above solution would work when importing different files on its own. This seems like I would need to know where the user's directory is and would have it preset to this directory before the button is even pressed.
Please do not mix the terms "listbox", "checkbox" and "button". You "uncheck the checkbox" and "When the button is checked" - confusing.
If you need the filename with the extension:
function [fPath, fName] = SuperFileParts(List)
if iscellstr(List)
fPath = cell(size(List));
fName = cell(size(List));
for k = 1:numel(List)
[fPath{k}, name, ext] = fileparts(List{k});
fName{k} = [name, ext];
end
elseif ischar(List)
[fPath, name, ext] = fileparts(List);
fName = [name, ext];
else
error('Input not handled.')
end
end
Took me a little bit to figure out the rest of my functions that were conflicting with my variable name I had for storing the full path but I got it to work! Thanks a ton for your help

Sign in to comment.

More Answers (0)

Categories

Asked:

on 9 May 2018

Commented:

on 15 May 2018

Community Treasure Hunt

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

Start Hunting!