Clear Filters
Clear Filters

Access matrix outside of for loop

1 view (last 30 days)
Hello everybody,
I count signals and co-localizations of these signals in big images. I do this with nested loops (I now it's not the optimal way how to do it) which make my code very slow. I want to use parfor to accelerate the run time.
I made a Matrix outside my main for loop in which I save the numbers. After the main for loop I take this Matrix and save it in an ecxel table. By using parfor I'm no longer abel to define my matrix outside of the loop. This gives me the problem that I can't acces my data in the end.
Is there a way to acces data generated in an for loop outside of it?
Thank you for any tips
imagefilesEpo = dir('*Alexa Fluor 488.tif');
imagefilesDCN = dir('*Alexa Fluor 647.tif');
nfiles=length(imagefilesEpo);
Matrix_with_Number_of_colocalization=zeros([nfiles 4]);
parpool(3)
parfor jj=1:nfiles
% Epo = imagefilesEpo(jj);
currentfilenameEpo = imagefilesEpo (jj).name;
currentimageEpo = imread(currentfilenameEpo);
Epo = currentimageEpo;
% DCN = imagefilesDCN(jj);
currentfilenameDCN = imagefilesDCN (jj).name;
currentimageDCN = imread(currentfilenameDCN);
DCN = currentimageDCN;
%% Epo und DCN zeigen
% figure('Name','Overlapping Epo - DCN, both unmodified')
% imshowpair(Epo, DCN)
%% blur
%Epo =imgaussfilt(Epo,5);
%% Threshold Epo with 0.35
Epo035 = imbinarize(Epo, 0.35);
%% max size Epo
Epo035max1900 = bwareafilt (Epo035, [1,1900]);
%% Threshold DCN with 0.25
DCN025 = imbinarize(DCN, 0.25);
%% Boundarys
% remove the % below only if you want to see the boarder on the picture
[Epo_R,Epo_C]=bwboundaries(Epo035max1900,'noholes');
% figure;
% imshow(Epo035max1900)
% hold on
% for k = 1:length(Epo_R)
% boundary = Epo_R{k};
% plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 0.002)
% end
[Marker_R,Marker_C]=bwboundaries(DCN025,'noholes');
% figure;
% imshow(DCN025)
% hold on
% for k = 1:length(Marker_R)
% boundary = Marker_R{k};
% plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 0.002)
% end
%% anzahl column in Matrix M = anzahl zusammenhängende elemente
rEpo=size(Epo_R,1);
rDCN=size(Marker_R,1);
%% anzahl pixel cxcl14015
pixelDCN025 = sum(DCN025(:) == 1);
%% Koordinaten der zusammenhängende Regionen von Epo als vektoren speichern
Koordinaten_Epo=zeros();
a=1;
b=2;
for i=1:rEpo
[E_row,E_col]=find(Epo_C==i);
for j=1:size(E_row)
Koordinaten_Epo(j,a)=E_row(j);
end
for k=1:size(E_col)
Koordinaten_Epo(k,b)=E_col(k);
end
a=a+2;
b=b+2;
end
%% Koordinaten von Marker in array speichern
Koordinaten_Marker=zeros();
[M_row,M_col]=find(Marker_C>=1);
for l=1:pixelDCN025
Koordinaten_Marker(l,1)=M_row(l);
Koordinaten_Marker(l,2)=M_col(l);
end
%% colocalization zählen
Num_colocalization=0;
o=1;
p=2;
while p <=size(Koordinaten_Epo,2)
for m=1:size(Koordinaten_Marker,1)
if p <=size(Koordinaten_Epo,2)
for q=1:size(Koordinaten_Epo,1)
if (Koordinaten_Marker(m,1) == Koordinaten_Epo(q,o)) && (Koordinaten_Marker(m,2) == Koordinaten_Epo(q,p))
Num_colocalization=Num_colocalization+1;
p=p+2;
o=o+2;
break
end
end
end
end
if p <=size(Koordinaten_Epo,2)
o=o+2;
p=p+2;
end
end
% saving the datas in a table
Matrix_with_Number_of_colocalization(jj,1)=Num_colocalization;
Matrix_with_Number_of_colocalization(jj,2)=rEpo;
Matrix_with_Number_of_colocalization(jj,3)=rDCN;
percentage=100/(rEpo/Num_colocalization);
Matrix_with_Number_of_colocalization(jj,4)=percentage;
row_name(jj)={strcat(extractBetween(currentfilenameEpo,1,29))};
end
% save the data
row_name=row_name';
RowName=cell2table(row_name);
Table_with_Number_of_colocalization=array2table(Matrix_with_Number_of_colocalization);
Colocalisation_EPO_DCN=[RowName,Table_with_Number_of_colocalization];
Colocalisation_EPO_DCN.Properties.VariableNames = ["Experiment","Colocalization","Epo","DCN","Colocalization in %"];
% save the final table as excel
filename = 'Colocalisation_EPO_DCN.xlsx';
writetable(Colocalisation_EPO_DCN,filename,'Sheet',1,'Range','A1');

Answers (1)

Sushranth Prakash Hebbar
Sushranth Prakash Hebbar on 5 Sep 2022
I understand that you want to access the 'Matrix_with_Number_of_colocalization' variable outside the 'parfor' loop.
When a 'parfor' loop is used, the variables are classified into different categories.
For more information on this, you can refer to the following documentation link:
  2 Comments
Alexandra Locherer
Alexandra Locherer on 5 Sep 2022
Hey thank you for the answer. I just wanted to close the question. I save my data with a function in to different files. These I can access from outside the parfor loop.
Sushranth Prakash Hebbar
Sushranth Prakash Hebbar on 5 Sep 2022
Yes. Once, the data has been saved in different files, it should be accessible even outside the 'parfor' loop.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!