Write table with with column names and subject ID

5 vues (au cours des 30 derniers jours)
Annabel Sorby-Adams
Annabel Sorby-Adams le 5 Sep 2023
Modifié(e) : Voss le 6 Sep 2023
Hi MATLAB team.
I am trying to export some calculated data. I would like to amend the cole below to add the subject ID to each column header (the prefix of the files being input - in nii.gz format in the compressed folder attached) and add row headers for each of the variables exported (size_in_cubic_mm; ADCint; FLAIRint; DWIint).
I am not sure if I need to compile into a matrix first or use writetable instead of xlswrite. Any advice greatly appreciated!
clear, clc
datadir = '/Users/annabellesorby-adams/Desktop/00_Checked_In';
d = dir([datadir filesep '*Lesion_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Infarct Ipsilateral Mask ' ]);
d = dir([datadir filesep '*Contralateral_Mask.nii.gz']);
disp(['Found ' num2str(length(d)) ' Infarct Contralateral Mask ' ]);
d = dir([datadir filesep '*HF_DWI_b1000_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
disp(['Found ' num2str(length(d)) ' HF DWI b1000 Nonlinear scans ' ]);
d = dir([datadir filesep '*HF_ADC_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
disp(['Found ' num2str(length(d)) ' HF ADC Nonlinear scans ' ]);
d = dir([datadir filesep '*LF-MRI_FLAIR.nii.gz']);
disp(['Found ' num2str(length(d)) ' LF FLAIR scans ' ]);
resultspath = '/Users/annabellesorby-adams/Desktop/00_Checked_Out';
for i = 1 : length(d)
disp(['Working on case ' num2str(i) ' of ' num2str(length(d)) ': ' d(i).name])
f = find(d(i).name=='_');
ipsimaskfile = [datadir filesep d(i).name(1:f(1)) 'Lesion_Mask.nii.gz'];
contramaskfile = [datadir filesep d(i).name(1:f(1)) 'Contralateral_Mask.nii.gz'];
adcfile = [datadir filesep d(i).name(1:f(1)) 'HF_ADC_Nonlinear_lncc5sx30vel_Coregistration.nii.gz'];
flairfile = [datadir filesep d(i).name(1:f(1)) 'LF-MRI_FLAIR.nii.gz'];
dwifile = [datadir filesep d(i).name(1:f(1)) 'HF_DWI_b1000_Nonlinear_lncc5sx30vel_Coregistration.nii.gz'];
IMmri = MRIread(ipsimaskfile);
CMmri = MRIread(contramaskfile);
Amri = MRIread(adcfile);
Fmri = MRIread(flairfile);
Dmri = MRIread(dwifile);
M = IMmri.vol > (max(IMmri.vol(:))/2);
A = double(Amri.vol);
F = double(Fmri.vol);
D = double(Dmri.vol);
size_in_cubic_mm(i) = sum(M(:)) * prod(IMmri.volres);
ADCint(i) = median(A(M));
FLAIRint(i) = median(F(M));
DWIint(i) = median(D(M));
data = [size_in_cubic_mm; ADCint; FLAIRint; DWIint];
xlswrite ([resultspath filesep 'results.csv'], data);
disp('All done!');
end

Réponse acceptée

Voss
Voss le 5 Sep 2023
Modifié(e) : Voss le 5 Sep 2023
Make data a cell array and you can include row and column headers as desired.
See below for how that might work. (I've also replaced the use of filesep for building file and directory names with the use of fullfile, the use of num2str with sprintf, and xlswrite with writecell.)
unzip test.zip
clear, clc
% datadir = '/Users/annabellesorby-adams/Desktop/00_Checked_In';
datadir = 'test';
d = dir(fullfile(datadir,'*Lesion_Mask.nii.gz'));
disp(sprintf('Found %d Infarct Ipsilateral Mask ',numel(d)));
Found 1 Infarct Ipsilateral Mask
d = dir(fullfile(datadir,'*Contralateral_Mask.nii.gz'));
disp(sprintf('Found %d Infarct Contralateral Mask ',numel(d)));
Found 1 Infarct Contralateral Mask
d = dir(fullfile(datadir,'*HF_DWI_b1000_Nonlinear_lncc5sx30vel_Coregistration.nii.gz'));
disp(sprintf('Found %d HF DWI b1000 Nonlinear scans ',numel(d)));
Found 1 HF DWI b1000 Nonlinear scans
d = dir(fullfile(datadir,'*HF_ADC_Nonlinear_lncc5sx30vel_Coregistration.nii.gz'));
disp(sprintf('Found %d HF ADC Nonlinear scans ',numel(d)));
Found 1 HF ADC Nonlinear scans
d = dir(fullfile(datadir,'*LF-MRI_FLAIR.nii.gz'));
disp(sprintf('Found %d LF FLAIR scans ',numel(d)));
Found 1 LF FLAIR scans
% resultspath = '/Users/annabellesorby-adams/Desktop/00_Checked_Out';
resultspath = 'test';
% pre-allocate results variable 'data':
Nd = numel(d);
% data is a cell array with 5 columns (4 data + 1 for the row headers)
% and Nd+1 rows (Nd for the files + 1 for the column headers)
data = cell(Nd+1,5);
% put the row headers in place:
data(1,2:5) = {'size_in_cubic_mm', 'ADCint', 'FLAIRint', 'DWIint'};
for i = 1 : Nd
disp(sprintf('Working on case %d of %d: %s',i,Nd,d(i).name))
f = find(d(i).name=='_');
% file prefix (?)
prefix = d(i).name(1:f(1));
ipsimaskfile = fullfile(datadir,[prefix 'Lesion_Mask.nii.gz']);
contramaskfile = fullfile(datadir,[prefix 'Contralateral_Mask.nii.gz']);
adcfile = fullfile(datadir,[prefix 'HF_ADC_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
flairfile = fullfile(datadir,[prefix 'LF-MRI_FLAIR.nii.gz']);
dwifile = fullfile(datadir,[prefix 'HF_DWI_b1000_Nonlinear_lncc5sx30vel_Coregistration.nii.gz']);
IMmri = MRIread(ipsimaskfile);
CMmri = MRIread(contramaskfile);
Amri = MRIread(adcfile);
Fmri = MRIread(flairfile);
Dmri = MRIread(dwifile);
M = IMmri.vol > (max(IMmri.vol(:))/2);
A = double(Amri.vol);
F = double(Fmri.vol);
D = double(Dmri.vol);
% put the file's results into data
data(i+1,:) = { ...
prefix(1:end-1) ... % column header (prefix without the trailing underscore)
sum(M(:)) * prod(IMmri.volres) ... % size_in_cubic_mm
median(A(M)) ... % ADCint
median(F(M)) ... % FLAIRint
median(D(M)) ... % DWIint
};
end
Working on case 1 of 1: A001_LF-MRI_FLAIR.nii.gz
% check the value of data:
disp(data)
{0×0 double} {'size_in_cubic_mm'} {'ADCint'} {'FLAIRint'} {'DWIint'} {'A001' } {[ 7.1465e+03]} {[ 808]} {[ 38723]} {[ 162]}
output_file = fullfile(resultspath,'results.csv');
writecell(data, output_file);
% xlswrite(output_file, data);
disp('All done!');
All done!
  5 commentaires
Annabel Sorby-Adams
Annabel Sorby-Adams le 6 Sep 2023
Thank you so much, as always, for your help and guidance!
Voss
Voss le 6 Sep 2023
Modifié(e) : Voss le 6 Sep 2023
You're welcome! Any other questions, let me know.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur EEG/MEG/ECoG dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by