hi there! so I have folders containing dicom files and bitmap files of the dicom data. the dicom files have names that corresponds with the bitmap files, with one dicom files corresponding to many bitmap files so for exampe, the dicom name is 00022.dcm while the corresponding bitmap files would be 2015220700022_Frame20.bmp, 2015220700022_Frame21.bmp and so on, the number after the frame marks which frame of the dicom sequence the bitmap picture is.
the bitmap files and dicom files are in different folders, and the dicom files are located in the subfolder while the input is in a different main folder but both are still in the same path.
what i want to do is to pair the dicom files with its corresponding bitmap in a new folder, a folder for each dicom name ( so eg. folder 00022 contains dicom file 00022.dcm and the bitmap sequences. I have tried a lot of the examples posted here to no avail please help!
trying to apply dir to dicom
% dicom files
mainfolder = 'Users/smn/Downloads/summer project/CL2 Dicom/Daniel';
subfolders = dir(fullfile(mainfolder, 'P*'));
subfolder_list = {subfolders(:).name}; % relevant filenames in folders
for i = 1 : length(subfolder_list)
mkdir( dicomname ) % dicom name hasnt been implemented yet
end
%for input
input = 'Users/smn/Downloads/summer project/input';
if ~isdir(input)
errmsg = sprintf('Error: The following folder does not exist:\n%s', input);
return;
end
seq = '*'; %sequence of bitmap file ( 1,2,34, etc)
I = dir( fullfile( input, '*_Frameseq.bmp')) % i want to group the files based on the asterisk in this %line
I = natsortfiles(I);
for k = 1: numel(I);
mkdir(F)
F = fullfile(input, I(k).name) % grouping bitmap files based on three digits before _,
end

 Respuesta aceptada

Ive J
Ive J el 13 de Jul. de 2021
You can try regexp or pattern:
dcm = "00022.dcm";
bmp = ["2015220700022_Frame20.bmp", "2015220700023_Frame20.bmp", "2015220700022_Frame23.bmp"];
% with pattern
ptt = digitsPattern + regexprep(dcm, '.dcm$', '') + "_";
matchedIdx = contains(bmp, ptt)
matchedIdx = 1×3 logical array
1 0 1
% with regexp
matchedIdx = ~cellfun(@isempty, regexp(bmp, "\d+" + regexprep(dcm, '.dcm$','') + "_"))
matchedIdx = 1×3 logical array
1 0 1

6 comentarios

sesilia maidelin
sesilia maidelin el 13 de Jul. de 2021
This works, thank you! I have another question though, this is to be applied on folders, so i tried applying this https://www.mathworks.com/matlabcentral/answers/437494-how-to-loop-through-all-files-in-subfolders-in-a-main-folder but it doesnt seem to work as the setdiff part only returns an empty cell.Do you have any suggestions on what to do? i'm a noob sorry
Ive J
Ive J el 13 de Jul. de 2021
Donno exactly how you are using that snippet, but works just fine for me. I have two subfolders in my current wd: "ADD" and "DOM":
D = pwd; % current directory
S = dir(fullfile(D,'*'));
setdiff({S([S.isdir]).name},{'.','..'})
ans =
1×2 cell array
{'ADD'} {'DOM'}
Make sure you are setting your target directory correctly. If setdiff returns empty cell, it simply means you don't have any subfolder in that directory.
sesilia maidelin
sesilia maidelin el 14 de Jul. de 2021
thank you so much! It's working now :)
sesilia maidelin
sesilia maidelin el 18 de Jul. de 2021
hi there, i have a question if u dont mind. what is the difference with using ' ' and " "? because im working with a file that is a cell array so dcm is {'00072.dcm'} {'00073.dcm'} and so on and bmp is also a cell array for {'2016250700072.bmp'} i am asking because the matched idx doesnt seem to work in my code. i attach my code here and it only returns the ans for matchedIdx when it returns a 0, while not moving any file that matches my dicom folder file name. ( it only prints out matchedIdx = 0, file xx does not have a matching dicom file and never returns matchedIdx = 0 and actually move the file)
for jj = 1 : numel(dcmlist)
[filepath, name, ext]= fileparts(dcmlist(jj));
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$'," ")+'_';
for kk = 1 :numel(bmplist)
matchedIdx = contains(bmplist{kk}, ptt)
if matchedIdx == true
movefile(fullfile(bmppath{kk},bmplist{kk}),fullfile( nufolderpath, name)) % has to be if matched!! this does not match
else
fprintf('file %s does not have a matching dicom file\n ', bmplist{kk});
end
end
Ive J
Ive J el 21 de Jul. de 2021
@sesilia maidelin can you set a breakpoint before your loop and save workspace to a mat file and upload it here?
save('mywdData.mat', 'dcmlist', 'bmplist', 'nufolderpath') % then upload mywdData.mat here
sesilia maidelin
sesilia maidelin el 21 de Jul. de 2021
thank you for your help earlier, i've managed to make it work :) turns out it's a fault within the source file.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre DICOM Format en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 12 de Jul. de 2021

Comentada:

el 21 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by