this is the whole code
function [ output_args ] = phase_equation( input_args )
% To delete the folder (Results) if it exists so we do not have to do it manually.
if exist('Results', 'dir')==7 system((sprintf('rm -r Results'))); end
% To create the folder (Results) containing our results.
if exist('Results', 'dir')==0 system((sprintf('mkdir Results'))); end
% To give the path of two folders which we want do the equation on.
folder_name1 = uigetdir('/home/gast/Desktop/'); folder_name2 = uigetdir('/home/gast/Desktop/'); folder_name3 = uigetdir('/home/gast/Desktop/'); folder_name4 = uigetdir('/home/gast/Desktop/');
% To read the contains of those folders we've choosen.
system((sprintf('ls "%s" -v | grep .IMA$ > list1.txt',folder_name1))); system((sprintf('ls "%s" -v | grep .IMA$ > list2.txt',folder_name2))); system((sprintf('ls "%s" -v | grep .IMA$ > list3.txt',folder_name3))); system((sprintf('ls "%s" -v | grep .IMA$ > list4.txt',folder_name4)));
% To put the contains of the folders in lists.
fid1 = fopen('list1.txt'); tline1 = fgetl(fid1); i=1; while ischar(tline1) filesStruct1(i).name=tline1; tline1 = fgetl(fid1); i=i+1; end fclose(fid1);
fid2 = fopen('list2.txt'); tline2 = fgetl(fid2); j=1; while ischar(tline2) filesStruct2(j).name=tline2; tline2 = fgetl(fid2); j=j+1; end fclose(fid2);
fid3 = fopen('list3.txt'); tline3 = fgetl(fid3); k=1; while ischar(tline3) filesStruct3(k).name=tline3; tline3 = fgetl(fid3); k=k+1; end fclose(fid3);
fid4 = fopen('list4.txt'); tline4 = fgetl(fid4); t=1; while ischar(tline4) filesStruct4(t).name=tline4; tline4 = fgetl(fid4); t=t+1; end fclose(fid4);
% To stop and exit the programm in case the number of the files in the % first folder does not math the number of the files in the second one.
sizeOfStruct1 = size(filesStruct1); sizeOfStruct2 = size(filesStruct2); sizeOfStruct3 = size(filesStruct3); sizeOfStruct4 = size(filesStruct4);
if sizeOfStruct1(1,2) ~= sizeOfStruct2(1,2)
msgbox('Number of images is not the same in the chosen folders!');
exit(1);
end
if sizeOfStruct3(1,2) ~= sizeOfStruct4(1,2)
msgbox('Number of images is not the same in the chosen folders!');
exit(1);
end
% Here we do any equation we want on the data we took.
for c=1:sizeOfStruct1(1,2)
name1 = fullfile(folder_name1,filesStruct1(c).name);
name2 = fullfile(folder_name2,filesStruct2(c).name);
name3 = fullfile(folder_name3,filesStruct3(c).name);
name4 = fullfile(folder_name4,filesStruct4(c).name);
% Read image from the first fcheckNplotolder, image from the second one,
% convert their matrices to double and store them in A and B.
inv1{c} = double(dicomread(name1));
inv1_pha{c} = double(dicomread(name2));
inv2{c} = double(dicomread(name3));
inv2_pha{c} = double(dicomread(name4));
alpha{c} = ((inv1_pha{c}.*pi)./(4096)) - (pi./2);
beta{c} = ((inv2_pha{c}.*pi)./(4096)) - (pi./2);
metadata = dicominfo(name1);
inv1_com{c} = inv1{c}.* exp(i.*alpha{c});
inv2_com{c} = inv2{c}.* exp(i.*beta{c});
N{c} = inv1_com{c}.* conj(inv2_com{c});
D{c} = abs(inv1_com{c}.^2) + abs(inv2_com{c}.^2);
R{c} = N{c}./D{c};
% dicomwrite(R,'Results',double, metadata);
end
save(Results,'R');
end