Plotting Data from a For Loop and If statement

So I have a complicated data structure. ParentFolder contains 33 "cases" inside each case there ranges from 1-3 separate DTI "scans". I want to go into those scans subfolders and get several specific files and then plot them in a variety of ways and I am struggling with how to combine the "if" statements and the for loop to be able to load the data in a way thats graphable. Right now everything is in character arrays, but maybe a struct is better? Here is my for loop I have so far:
cases = dir([casedir '*Casenames*']); %this displays the 33 cases inside the parent folder
for cas= 1:length(cases);
casefold=([casedir cases(cas).name filesep]);
pexfold1= ([casefold '*V01*']); %the different conditions
pexfold2= ([casefold '*V02*']);
postexfold=([casefold '*V03*']);
if exist ('pexfold1');
dtipex1fa=([pexfold1 filesep 'dti' filesep 'fastats.txt' filesep]);
dtipex1adc=([pexfold1 filesep 'dti' filesep 'adcstats.txt' filesep]);
T1FA= ([pexfold1 filesep 'dti' filesep 'Anatomy 3.fa.txt' filesep]);
T1ADC=([pexfold1 filesep 'dti' filesep 'Anaomty 3.adc.txt' filesep]);
else
dtipex1fa=[];
dtipex1adc=[];
T1FA=[];
T1ADC=[];
end
if exist ('pexfold2');
dtipex2fa=([pexfold2 filesep 'dti' filesep 'fastats.txt' filesep]);
dtipex2adc=([pexfold2 filesep 'dti' filesep 'adcstats.txt' filesep]);
T2FA= ([pexfold2 filesep 'dti' filesep 'Anatomy 3.fa.txt' filesep]);
T2ADC=([pexfold2 filesep 'dti' filesep 'Anatomy 3.adc.txt' filesep]);
else
dtipex2fa=[];
dtipex2adc=[];
T2FA=[];
T2ADC=[];
end
if exist ('postexfold');
dtipostfa= ([postexfold filesep 'dti' filesep 'fastats.txt']);
dtipostadc= ([postexfold filesep 'dti' filesep 'adcstats.txt']);
TPostFA= ([postexfold filesep 'dti' filesep 'Anatomy 3.fa.txt']);
TPostADC=([postexfold filesep 'dti' filesep 'Anatomy 3.adc.txt']);
else
dtipostfa=[];
dtipostadc=[];
TPostFa=[];
TPostADC=[];
end
end %then ideally I would graph pexfold1 vs pexfold2 vs postexfold and perform statistical tests
Anatomies = {'Anatomy 1';'Anatomy 2';'Anatomy 3'};
Times = {'Condition 1'; 'Condition 2'; 'Condition 3'};
What happens right now it says there is always a pexfold1,2 and postexfold no matter what, but those are the folders that vary in existance between cases. I am trying to graph the fa values of each condition against each other, adc against each other and the stats against each other. Inside each of the "fastats" and "adcstats" txt files there are 3 different anatomical landmarks which have mean DTI values to graph. The Anatomy 3 txt files only have 1 anatomical landmark. I am just tripped up on how to progress through this if the file in question doesn't exist and how to get to the data when its stuck in a character array. Thank you for reading my long post and for your help!

7 件のコメント

Stephen23
Stephen23 2020 年 8 月 7 日
編集済み: Stephen23 2020 年 8 月 7 日
@Natha Davis: you really should be using fullfile, rather than messing about with string concatentation and filesep.
dpb
dpb 2020 年 8 月 7 日
...
casefold=([casedir cases(cas).name filesep]);
pexfold1= ([casefold '*V01*']); %the different conditions
...
if exist ('pexfold1');
...
is testing the existence of the variable pexfold1 which you just created above so it will always return nonzero (true). In reality it will return 1 indicating a variable in the workspace.
What you're intending to test is whether the subdirectory exists
casefold=fullfile(casedir,cases(cas).name);
pexfold1= fullfile(casefold,'*V01*');
if exist(pexfold1)==7 % test explicitly for the folder/subdirectory
...
Use the builtin fullfile function instead of all the string catenation operators -- it handle the system separator and checks for the existence of which when doing the creation to either add if needed or not if already in the string. Just makes code much easier to read, debug.
Could write more generic code to build the subdirectory names dynamically as well but that's another refinement.
As far as how to read the data; we would need to see the files you're trying to read to know what's in them; there's no reason that whatever numeric data are contained therein can't be read as numeric or parsed to convert to numeric but can't say how without knowing what form it is in.
Natha Davis
Natha Davis 2020 年 8 月 7 日
Thank you dpb, that is a good suggestion and makes things a lot cleaner. I am still having trouble getting the correct "exist" answer though. I currently have:
For cas= 3%:length(cases);
casefold=fullfile(casedir,cases(cas).name);
pexfold1= fullfile(casefold,'*V01*');
pexfold2= fullfile(casefold,'V02*');
postexfold=fullfile(casefold,'*V03*');
if exist(pexfold1)==7 % test explicitly for the folder/subdirector
dtipex1fa= fullfile(pexfold1,'dti','fastats.txt');
dtipex1adc=fullfile(pexfold1,'dti','adcstats.txt');
T1FA= fullfile(pexfold1,'dti','Anatomy.fa.txt');
T1ADC=fullfile(pexfold1,'dti','Anatomy.adc.txt');
else dtipex1fa=[];
dtipex1adc=[];
T1FA=[];
T1ADC=[];
end
Which will end up just creating empty [] even if the file exists. case "3" has V01 missing, but has V02 and V03, but will return empty [ ] for all 3 and give T1FA=[ ], T2FA= [ ] etc.
dpb
dpb 2020 年 8 月 7 日
The problem is using the wildcards in exist -- it doesn't use the OS dir() function but treats the name as a literal. I happen where am sitting at command line to have:
>> dir *.
. .. ColvinPropertiesDonation InvestSched
>> exist('Invest*')==7
ans =
logical
0
>> exist('InvestSched')==7
ans =
logical
1
>>
where you see the two subdirectories as the files w/o extensions. NB: the wildcard fails, the exact match succeeds.
Either use dir() and then the .isdir field and match name of interest or write your other logic to do exact search.
I've not tried to read the code in sufficient depth to really understand precisely what you are/aren't trying to isolate so not sure if there might be a somewhat different search strategem or not. A list of what the directory structure actually is might help folks to visualize to see if that strikes up other ideas.
Natha Davis
Natha Davis 2020 年 8 月 7 日
I appreciate your help again. I guess I cant use the wildcard then, but I kind of need to since every "V01" or "V02" iteration has a different preceeding label that is unique. I don't feel I can show the data with sufficient privacy at this stage, sorry.
I started all of this because the author of the code previously was hardcoding in every single file iteration (case 1, v01. case 1 v02 etc) which was taking forever, but after spending a while on this I just bit the bullet and started doing that.
So now I have a loop that identifies the V01 iterations as follows:
l = l+1;cases(l).fold='Case 1/V01';cases(l).group=1;
l = l+1;cases(l).fold='Case 1/V02';cases(l).group=1;
l = l+1;cases(l).fold='Case 2/V01';cases(l).group=2;
l = l+1;cases(l).fold='Case 2/V02';cases(l).group=2;
Inside each of those "V01, V02" are the serveral different text files I am trying to graph and test against each other, but how do I graph them as a group and then move onto the next group which may have more or less versions (v01, v02, v03)?
I have this loop:
for c = 1:length(cases);
dtifold = filestar([basef cases(c).fold filesep 'dti' filesep ],true); %sorry this was before you got me doing "fullfile"
Tfafile = filestar([dtifold 'anatomy.fa.txt']);
% read results
fid = fopen(Tfafile);
% dat = textscan(fid,' [ %d ] %f %f %f %f %f %d ',1,'HeaderLines',2,'Delimiter',' ','MultipleDelimsAsOne',1);
dat = textscan(fid,'[%d]%f%f%f%f%f%d',1,'HeaderLines',1,'Delimiter',' ','MultipleDelimsAsOne',1);
fclose(fid);
TFA=dat{2}; %this grabs the value that I want to graph
end
So I am able to grab one value from each case using this loop, but I am confused as to how then graph this value with the other 1-2 values that may exist that correspond to the same case, but different iteration. I was thinking something like this but it isn't working:
vol = unique([cases(:).group]);
for i = 1:length(vol)
plot(i(TFA));
end;
dpb
dpb 2020 年 8 月 8 日
Hmmm....I'm sure there's a way to automate the generation other than physically writing each manually. But, I don't have the stamina to continue tonight to try to figure out just what the result is that needs discerning clearly enough in my mind to actually write code.
I do think it would help immeasurably if you could just list the directory structure of the files so can actually see it--not the data in the files but the file storage structure/naming conventions. That would also illustrate by example the disconnect you're trying to code around.
Just otomh, it seem like simply traversing the directory tree for the directory entries and processing them would be fairly straightahead route where one can pass the wildcard to the OS for matching the directories. But, seeing it all laid out in front of us would help immeasurably. Remember you know all this and it's clear to you; we can't see your terminal so know only what is written on the message screen here.
Once we can work through that part of the problem, it will probably be clear where the data are that are needed and how to consolidate them as needed--quite likely a cell array might be an intermediary that then gets manipulated to put things together as needed to plot the necessary variables against the desired...
But, I'm wore out tonight, sorry... :)
Natha Davis
Natha Davis 2020 年 8 月 10 日
No problem! I apologize not being able to show as much information as you need, I know its frustrating and belive me I am frustrated as well. I can write out the general file structure here and see if that helps:
My Downloads Folder
Folder with 30 Cases (Parent Folder)
Each of those cases has 1 to 3 different Versions (V01, V02, V03). These versions correspond to different time points in the same case. Think of 1 as before, 2 as during and 3 as after.
Inside each of those version folders is a folder called "dti"
Inside each "dti" are 2-3 "txt" files corresponding to different ROIs highlighting certain anatomical regions
Each txt file looks like the attached picture
Out of the values in the file I want to graph and test the means from these different versions (V01, 02, 03) on the same graph. So each of the anatomical region graphs would have data from each of the time points. The graph that corresponds to one anatomical area would show "before, during, after". Then a different graph for a different ROI shows "before, during, after" etc.
So what I ended up doing in my last comment was just point MATLAB to ALL of the cases and versions and then group them by case. "Case 1 V01, group.1" "Case 1 V02, group.2" "Case 2 V01, group.2" "Case 2 V02, group.2" etc. But I feel like there is definitely a more efficient way and dynamic way so that when we are able to get more timepoints for some of these that only have 1-2 versions we can easily detect them instead of having to write out every single individual case. I hope that helps! I apologize for the secrecy, but thank you so much for your continued help!

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 8 月 6 日

コメント済み:

2020 年 8 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by