MATLAB Answers

How to get upper foldername?

2 views (last 30 days)
Nik Rocky
Nik Rocky on 24 Jun 2020
Commented: Nik Rocky on 24 Jun 2020
Hello,
subfoldername = '/home/user/workspace/QT/Software_2.0_QT/IO/Motor_Set_1/' %changes every loop
foldername = ?
How to get:
'/home/user/workspace/QT/Software_2.0_QT/IO/'
P.S. independent of current folder
Thanks!

  1 Comment

KSSV
KSSV on 24 Jun 2020
You should already be having it in hand..isn't it?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 24 Jun 2020
I put fileparts() into a function called GetParentFolder() because I use it so often. I've attached it.

  3 Comments

Nik Rocky
Nik Rocky on 24 Jun 2020
Thank you, but it not working by me:
FilePath = '/home/user/workspace/QT/Software_2.0_QT/IO/Motor_Set_1/AKG_C1000S/Distance_0.5m/Scenario_M1/001_m1_60_const_20200611_200515/001_m1_60_const_20200611_200515_SNR_F1/'
GetParentFolder(FilePath)
ans =
'/home/nikitajarocky/workspace/QT/Software_2.0_QT/IO/Motor_Set_1/AKG_C1000S/Distance_0.5m/Scenario_M1/001_m1_60_const_20200611_200515/001_m1_60_const_20200611_200515_SNR_F1/'
i change also msgboxw(message); to msgbox(message);, because it was a MATLAB error.
I have MATLAB R2018a
Image Analyst
Image Analyst on 24 Jun 2020
Sorry, that was written before MATLAB had the strsplit() function so I used a FileExchange function. So I've completely redone the function and attached it. It will also handle some rare stress cases properly. Here is a variety of test cases to check if it worked correctly.
[parentFolder, childFolder] = GetParentFolder('D:/aaa/bbb') % No trailing slash
[parentFolder, childFolder] = GetParentFolder('D:/aaa/') % Treailing slash
[parentFolder, childFolder] = GetParentFolder('D:/aaa')
[parentFolder, childFolder] = GetParentFolder('D:/') % Root folder.
f = fullfile(pwd, 'a.txt'); % What happens if we pass in a file (that exists) instead of a folder? It still works!
[parentFolder, childFolder] = GetParentFolder(f)
% Try a folder with a dot in the deepest folder name so it looks like a filename might look.
folderName = 'D:\OneDrive\Matlab\work\Tests\level1.level2' % Folder must exist for you to test this case.
[parentFolder, childFolder] = GetParentFolder(folderName)
I wanted a general purpose function where I got both the parent folder, and the child (deepest) folder. Note that if you simply use
[parentFolder, childFolder] = fileparts(startingFolder);
and pass in a filename, you won't get the child folder correctly since it would return the base file name of the file as the child folder. Plus if you have a folder where the deepest folder has a dot in the name it won't work then either. So I put in a few lines of code to handle those cases.
%==========================================================================================================================
% Gets the folder one level up. If startingFolder is a filename with an extension it gets the containing folder and the child folder is null.
% Returns null for both if the folder does not exist, and it's not a filename either.
function [parentFolder, childFolder] = GetParentFolder(startingFolder)
parentFolder = []; % Initialize
childFolder = [];
try
if isfolder(startingFolder)
[parentFolder, childFolder, ext] = fileparts(startingFolder);
% Need to append extension for rare cases where deepest folder has a dot in the name.
childFolder = [childFolder, ext];
elseif isfile(startingFolder)
% It's a filename, not a folder. Need to change otherwise childFolder will be returned as the base file name.
[parentFolder, childFolder, ext] = fileparts(startingFolder);
childFolder = []; % No child folder since it's a filename, not a folder name.
end
catch ME
message = sprintf('Error in GetParentFolder():\n%s', ME.message);
uiwait(msgbox(message));
end
return; % from GetParentFolder
end
Nik Rocky
Nik Rocky on 24 Jun 2020
Wow, its works, thank you very much! You do a great job and I'm very helpful for great description and attachment!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!