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

96 vues (au cours des 30 derniers jours)
BW5217
BW5217 le 9 Mai 2018
Commenté : BW5217 le 15 Mai 2018
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

Réponse acceptée

Jan
Jan le 9 Mai 2018
Modifié(e) : Jan le 9 Mai 2018
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 commentaires
Jan
Jan le 9 Mai 2018
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
BW5217
BW5217 le 15 Mai 2018
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by