Select max files in a struct
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Brandon Gunasekera
am 21 Jul. 2020
Kommentiert: Brandon Gunasekera
am 23 Jul. 2020
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
Akzeptierte Antwort
Will Wilson
am 21 Jul. 2020
Hi Brandon,
You might consider a newer approach to this problem that leverages datastore functionality (vs. "dir" like you are using) as well as the new string datatype.
If I were doing this work I would create a table that contains the fullfilepath of each file and then use some combination of string methods to parse the file paths to get the ones I wanted (and ignore those I don't need).
TopLevelFilePath = "/data/project/CANDOWN/bg/";
FileSet = resolve(matlab.io.datastore.DsFileSet(TopLevelFilePath,...
'IncludeSubfolders',true,... % Based on your question you probably need this argument
'FileExtensions','.mat'); % Configure the File type if it helps you - use this to ignore files you don't care about if you can
At this point you will have an [m x 2] table that has columns "FileName" and "FileSize". From here you would can grab all of column 1 and get to work on it. Recoginze that this is an array of strings.
FNames = FileSet.FileNames;
To get a sense of what you can do with strings (and they are awesome) run this at the command line to see what methods are avaialbe to you out of the box:
methods('string')
The important things to note here are:
1.) What is avaiable to you in terms of functionality (lots of really useful functions)
2.) That you can mix and match and combine as much as you need to
It is at this point where you need to get creative and figure out how to best parse what you want to get to. Say you want to get the file called "CANDOWN009A" in the following set of files. Here's an example you can run to see how this flows
FNames = [...
"/data/project/CANDOWN/bg/CANDOWN002A";
"/data/project/CANDOWN/bg/CANDOWN003B";
"/data/project/CANDOWN/bg/CANDOWN004A";
"/data/project/CANDOWN/bg/CANDOWN005B";
"/data/project/CANDOWN/bg/CANDOWN009A"];
% Get everthing before the last character
str = extractBefore(FNames, strlength(FNames))
% Get everything after the common path
str = extractAfter(str, "/data/project/CANDOWN/bg/CANDOWN")
% Get the index of the max val in the filename
[~,idx] = max(double(str))
% Finally, get the filename you care about
MaxFile = FNames(idx)
% If you prefer everything in one call it looks like this:
% [~,idx] = max(double(extractAfter(extractBefore(str, strlength(str)), "/data/project/CANDOWN/bg/CANDOWN")))
% MaxFile = FNames(idx)
I hope this opens up some new ideas for you. What I have shown here is very flexible and should serve you well when it comes to slicing and dicing files and paths.
3 Kommentare
Will Wilson
am 22 Jul. 2020
Hi Brandon,
My pleasure. No apologies - this is a great question. Off the top of my head I can think of 2 things this might be:
1.) This feature (matlab.io.datastore.DsFileSet) came out in MATLAB R2017b so if you are using a version of MATLAB that is older than that this code won't work (because the feature didn't exist before 17b).
2.) You might need to tweak your path to make sure it is a fully qualified path. From your original question, it looks like you are on Windows. If yes, try the full path - something like "C:/data/project/CANDOWN/bg/".
I hope this resolves the error you saw.
-Will
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!