Help making my code more efficient

1 view (last 30 days)
Alex Chagovetz on 20 Jan 2022
Commented: Kevin Holly on 21 Jan 2022
So I have the below code I use for post-2P image proccessing. I am fairly new to Matlab so I apologize if this is a rudamentatry question and I would appreciate guaidance more so than an answer I can copy and paste. Bsacilly most of the code I feel like could be made into a nested loop but I am not quite sure how to structure that. Thanks in advance!!
x=2590
for ii=1:x
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==1)
imwrite(QQ,['E1_1018_1' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_1' '.tif'],'tif','writemode','append');
end
end
for ii=x+1:x*2
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==x+1)
imwrite(QQ,['E1_1018_2' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_2' '.tif'],'tif','writemode','append');
end
end
for ii=(x*2)+1:x*3
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*2)+1)
imwrite(QQ,['E1_1018_3' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_3' '.tif'],'tif','writemode','append');
end
end
for ii=(x*3)+1:x*4
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*3)+1)
imwrite(QQ,['E1_1018_4' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_4' '.tif'],'tif','writemode','append');
end
end
for ii=(x*4)+1:x*5
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*4)+1)
imwrite(QQ,['E1_1018_5' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_5' '.tif'],'tif','writemode','append');
end
end
for ii=(x*5)+1:x*6
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*5)+1)
imwrite(QQ,['E1_1018_6' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_6' '.tif'],'tif','writemode','append');
end
end
for ii=(x*6)+1:x*7
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*6)+1)
imwrite(QQ,['E1_1018_7' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_7' '.tif'],'tif','writemode','append');
end
end
for ii=(x*7)+1:x*8
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*7)+1)
imwrite(QQ,['E1_1018_8' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_8' '.tif'],'tif','writemode','append');
end
end
for ii=(x*8)+1:x*9
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*8)+1)
imwrite(QQ,['E1_1018_9' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_9' '.tif'],'tif','writemode','append');
end
end
for ii=(x*9)+1:x*10
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*9)+1)
imwrite(QQ,['E1_1018_10' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_10' '.tif'],'tif','writemode','append');
end
end
for ii=(x*10)+1:x*11
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*10)+1)
imwrite(QQ,['E1_1018_11' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_11' '.tif'],'tif','writemode','append');
end
end
for ii=(x*11)+1:x*12
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*11)+1)
imwrite(QQ,['E1_1018_12' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_12' '.tif'],'tif','writemode','append');
end
end
for ii=(x*12)+1:x*13
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*12)+1)
imwrite(QQ,['E1_1018_13' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_13' '.tif'],'tif','writemode','append');
end
end
for ii=(x*13)+1:x*14
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*13)+1)
imwrite(QQ,['E1_1018_14' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_14' '.tif'],'tif','writemode','append');
end
end
for ii=(x*14)+1:x*15
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*14)+1)
imwrite(QQ,['E1_1018_15' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_15' '.tif'],'tif','writemode','append');
end
end
for ii=(x*15)+1:x*16
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*15)+1)
imwrite(QQ,['E1_1018_16' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_16' '.tif'],'tif','writemode','append');
end
end
for ii=(x*16)+1:x*17
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*16)+1)
imwrite(QQ,['E1_1018_17' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_17' '.tif'],'tif','writemode','append');
end
end
for ii=(x*17)+1:x*18
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*17)+1)
imwrite(QQ,['E1_1018_18' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_18' '.tif'],'tif','writemode','append');
end
end
for ii=(x*18)+1:x*19
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*18)+1)
imwrite(QQ,['E1_1018_19' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_19' '.tif'],'tif','writemode','append');
end
end
for ii=(x*19)+1:x*20
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if (ii==(x*19)+1)
imwrite(QQ,['E1_1018_20' '.tif'],'tif');
else
imwrite(QQ,['E1_1018_20' '.tif'],'tif','writemode','append');
end
end

Kevin Holly on 21 Jan 2022
Below I created a nested loop as requested. If the comments do not provide enough guidance, please let me know.
x = 2590;
for i = 1:20
for ii=x*(i-1)+1:x*i % modified, replaced the numbers you had with equation
QQ=squeeze(Q(ii,:,:));
QQ=cell2mat(QQ);
QQ=reshape(QQ,512,796);
if ii == x*(i-1)+1 % modified, replace number you had in each iteration with i-1
imwrite(QQ,['E1_1018_' num2str(i) '.tif'],'tif'); % Replaced hardcoded numbers with a num2str function that converts i from a number to a string
else
imwrite(QQ,['E1_1018_' num2str(i) '.tif'],'tif','writemode','append');
end
end
end
Kevin Holly on 21 Jan 2022
No problem, I would appreciate if you could accept the answer.

R2021a

Community Treasure Hunt

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

Start Hunting!