Select max files in a struct
Show older comments
I have 30 patients who underwent a brain scan.
Each patient has a directory that is named "CANDOWN" with a unique ID at the end.
Within each patient directory, there are a further 3 directories. Two of these directories are numbered. These are of interest. One direcotry is named "DICOM" which I am not inerested in.
I want my Matlab script to automatically detect the max and min directories, and then select all of the files within the directories respectively. For ease I will focus only on the max directory.
--------------
I ran this code:
--------------
name=2;
allT1=[];
T1 = dir(['/data/project/CANDOWN/bg/CANDOWN00',num2str(name),'A']) %A is because half my patients took a drug. The other half a placebo. A= drug. B= placebo
dirT1 = max(allT1)
--------------
These are my results:
--------------
T1 =
5x1 struct array with fields:
name
date
bytes
isdir
datenum
dirT1 =
[]
----------
This is not correct however, as the answer should be 0011
---------
When I put "T1.name" in the command window, I see the correct directories. When I try max (T1.name) for instance, I get the error
Error using max
Too many input arguments.
---------
I have read elsewhere that my issue is that I can not max and min a struct? It must be an index?
Any suggestions would be useful.
Many thanks,
Brandon
3 Comments
Walter Roberson
on 21 Jul 2020
Are you trying to extract the number from the file names and max those?
You appear to be using leading zeros on the numbers. In such a case, sort() of the file names also ends up sorting the numbers.
Brandon Gunasekera
on 21 Jul 2020
Walter Roberson
on 22 Jul 2020
max(str2double(regexp({T1.name}, '\d+', 'match')))
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!