0

I have a path (without a file name at the end) as a string in Matlab and i want to receive the first parent directory (the directory after the last file seperator character) in it.
At the moment i'm doing it like this:

>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = strsplit(filePath , filesep);
>>firstParent = firstParent{end};
>>disp(firstParent);
DOF


Is there any way i can use strsplit's return value (a cell array) without assinging it to a variable first?
Something like:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = ( strsplit(filePath , filesep) ){end};
>>disp(firstParent);
DOF
Semih SÜZEN
  • 163
  • 1
  • 2
  • 14

1 Answers1

2

Do you mean:

[~,firstParent] = fileparts ( 'D:\TRAIN_DATA\OBSTACLES\DOF' )
matlabgui
  • 5,642
  • 1
  • 12
  • 15
  • Yes this solves the specific example i gave to my question. I can receive the first parent directory with this statement. But there are other examples of this problem in my code that i'm actually trying to get the answer. Can i get the n. element of a cell array returned from a function like: `nThElement = aFunctionThatReturnsACellArray(){n};` Not like: `nThElement = aFunctionThatReturnsACellArray(); nThElement = nThElement{n};` – Semih SÜZEN Aug 15 '17 at 13:05
  • 2
    For the general case, I don't know of any _elegant_ way to do this without using an intermediate variable, but there are [a number of ugly or undocumented ways](https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it) such as using `subsref`, `getfield` or intermediate functions. – Tom Aug 15 '17 at 13:30
  • 2
    There is no elegant way, it isn't a feature of the MATLAB language, parentheses `()` must be at the end of a statement if used. This is possible in Octave, but that's a whole other can of worms. – Wolfie Aug 15 '17 at 13:31