First iteration of cell array ok, then it is empty until manually "evaluate selection" of previous segment
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I posted about this script earlier but it was quite long and I am unable to provide the data due to size limitations. However, I think I may have found the problematic segment early on in the script. Basically, the script pulls ID numbers from a csv, then matches those ID numbers with other data in the main source_path folder. So, the goal is to match an ID number with various .ima files, and then a .nii file, then run a software command. This works fine for the first iteration, but fails in the second. I think the issue is in this first portion of the code- specifically at the variables NII_list, NII_foldername, tmp_nii, and tmp_nii_foldername..
At the second iteration the script stops, and when I index tmp_nii and tmp_nii_foldername they come up empty. Whereas in the first iteration tmp_nii matched the ID and was able to find the nii file associated with that participant ID. The participant ID (cur_scan_code) changes correctly with the second iteration. However, when I go up and manually "evaluate selection" in the command window for
NII_foldername = cell(length(NII_list),1); %cell struct w length (rows) of nii_list x 1 column
for i = 1:length(dicom_list) %for the length of ^, get ith name from dicom list
dicom_foldername{i} = dicom_list(i).name; %setting dicom_foldername to the name of the list. This gives us contents
%of the folder where the dicoms are (source_path)
end
for i = 1:length(NII_list)
NII_foldername{i} = NII_list(i).name; %setting NII_foldername (which is empty before this) to contain the names/files from NII_list
%after this NII_foldername should have the nii files (ex 'RAN133-1a3_T1.nii')
end;
and then index tmp_nii and tmp_nii_foldername in the command window, I get the correct values. That is, the correct .nii file matched from the correct iteration of participant ID. The relevant portion of the code is below.
Thanks for the help. I hope this makes sense, I am quite stuck. I just don't know why it works after "evaluate selection" and not in the script. Is something causing this?
clc;
clear;
addpath (pwd);
%%Paths- source and final
source_path = 'B:\BBA\Gannet\Tests\8.13.18_testManualBatchScript'; %where the dicoms are; Can be a larger list, even if we are currently only doing a newer/smaller
%batch. The excel list document will take care of only what we want
%fit_path = 'B:\BBA\Gannet\TAP\TEMP\GannetFit_output'; %used for renaming MRS_struct
MRS_struct_path = 'B:\BBA\Gannet\Tests\8.13.18_testManualBatchScript'; %location of MRS_struct file that will be renamed
NII_path = 'B:\BBA\Gannet\Tests\8.13.18_testManualBatchScript\NII_files'; %folder of NII files
scan_code_filename = 'List_to_analyze.csv'; % this is the .csv we look through to get scan codes
% change per analysis, keep this in VOI folder
%%Keywords + setup
%analysis_name = 'BBA_ALC'
study_name = 'RAN'; %change per study/ analysis
%These keywords are from the DICOM files- to set file_water and file_metab
water_keyword = 'TEMP GABA H2O svs_se'; %these should be the same for RAN vs TAP etc.
metab_keyword = 'TEMP GABA svs_edit_RFA'; %most of them with this
NII_keyword = '_T1'; %this may work but need to test
%metab_keyword = 'TEMP GABA svs_edit_859A'; %use this for PRE RAN 69
% count = 0;
%%read the scan code
cd(source_path);
% look through CSV file. Open, look through (comma delim), close
fid = fopen(scan_code_filename);
scan_code_file_text = textscan(fid, '%s', 'Delimiter', ',');
fclose(fid);
% extract the list of scan codes from the CSV file
scan_code_exp = strcat(study_name,'\d\d\d-\d');
scan_code_array = regexp(scan_code_file_text{1},scan_code_exp, 'match'); %look for scan_code_exp within scan_code_file_text (.csv file)
%list of PIDs from .csv file
%put scan codes into final array
scan_code_array = scan_code_array(~cellfun('isempty', scan_code_array)); %making a usable array here and below
for i = 1:length(scan_code_array) %for loop to deal with cell array structure?
scan_code_array{i} = scan_code_array{i}{1};
end
scan_code_array = unique(scan_code_array); %make sure each ID is unique
%%Go to dicom location and find files-- LOOP Start
cd(source_path)
display ('Find DICOM files and set water/metab files');
% make an array for dicom folder name
dicom_list = dir(source_path); %top of layer where all dicom FOLDERS are stored (in TEMP/INS folders)
%DICOM folders
dicom_foldername = cell(length(dicom_list),1); %cell struct w length of dicom list X 1
% make an array for nii folder name
NII_list = dir([NII_path, '/*.nii']);%list all conent from location of NIFTI files
%%%NII_list = dir(NII_path); %location where nii files are
NII_foldername = cell(length(NII_list),1); %cell struct w length (rows) of nii_list x 1 column
for i = 1:length(dicom_list) %for the length of ^, get ith name from dicom list
dicom_foldername{i} = dicom_list(i).name; %setting dicom_foldername to the name of the list. This gives us contents
%of the folder where the dicoms are (source_path)
end
for i = 1:length(NII_list)
NII_foldername{i} = NII_list(i).name; %setting NII_foldername (which is empty before this) to contain the names/files from NII_list
%after this NII_foldername should have the nii files ('RAN133-1a3_T1.nii')
end;
% find the files
for i = 1: length(scan_code_array) %length of # of scancodes in list/array (.csv input file)
display ('inside for loop');
cur_scan_code = scan_code_array{i}; %current scan code is the ith of scan code array
%Setup dicom search via cur_scan_code (PID)
tmp = strfind(dicom_foldername, cur_scan_code); %find current scan code ^ (PID) within dicom folders
tmp_foldername = dicom_foldername(~cellfun('isempty', tmp)); %folders from with MATCHING SCAN CODE (should return metab and water)
%Setup nii search via cur_scan_code (PID)
tmp_nii = strfind(NII_foldername, cur_scan_code); %find current scan code ^^ (PID) within folder with NII files
tmp_nii_foldername = NII_foldername(~cellfun('isempty',tmp_nii)); %folders from ^ scan code (should return .nii file)
1 个评论
Amal George M
2018-8-28
Hi Holly,
This issue might be originating from the last ' for loop' in the shared code. Check whether any of the involved variables 'NII_foldername' or 'scan_code_array' are being modified inside the loop. Try debugging the 'for loop' by inserting breakpoints.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!