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 件のコメント
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
2020 年 8 月 7 日
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
2020 年 8 月 7 日
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
2020 年 8 月 10 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
