loop over nested functions overwrites variables
Ältere Kommentare anzeigen
I have written a routine for image processing and want to get the same output for a stack of pictures in the format (:,:,i) with i being the number of the picture (e.g. picture number 30 to 35). However, the output will only have information for the final i(:,:,35) and (:,:,30) to (:,:,34) will be empty, that is they have been overwritten. I tried a smaller loop to get the idea
zMin=30; zMax=35;
while zMin <= zMax
for i = zMin
a(1,1,i)=zMin
zMin=zMin+1;
end;
end;
and here it works perfectly fine. However, in my code consisting of nested functions, it doesn't, so I assume it has something to do with the format of the code:
function [firstScanRaw, mask, maskedImage, Ipores, Iskeleton, PropsLCCP, PropsLCCS, PoresBackbone, SkeletonBackbone, Porosity, Skeleton, nPixelROI, SkeletonObjects, PoreObjects, EulerRealisationsSkeleton, EulerRealisationsPores,PoreWidthRealisations, SkelWidthRealisations ] = porous_main(sampleName, zMin, zMax, display);
[firstScanRaw] = porous_load_input (sampleName,zMin);
[mask, maskedImage] = porous_polygon (firstScanRaw, display, sampleName);
while (zMin <= zMax)
for i = zMin;
[firstScanRaw] = porous_load_input (sampleName,zMin);
[mask, maskedImage] = porous_polygon_choosen (firstScanRaw, display, sampleName, mask,i);
[Ipores, Iskeleton] = porous_binarisation (maskedImage, display,i);
[PropsLCCP, PropsLCCS] = porous_elements(Ipores, Iskeleton, display, i);
[PoresBackbone, SkeletonBackbone, Porosity, Skeleton, nPixelROI, PoreDist, SkelDist] = porous_sizes (Iskeleton, Ipores, display, mask, maskedImage,i);
[Porosity, Skeleton, SkeletonObjects, PoreObjects, EulerRealisationsSkeleton, EulerRealisationsPores, PoreWidthRealisations, SkelWidthRealisations ] = porous_output(Porosity, Skeleton, PoreDist, SkelDist,PoresBackbone, SkeletonBackbone, nPixelROI, sampleName, PropsLCCP, PropsLCCS,i);
zMin=zMin+1;
end;
end;
end
3 Kommentare
Please format your code properly on this forum, because currently that code is unreadable. It is really very easy: select the code, and then press the {} Code button that you will find above the textbox.
Your function currently has eighteen (18) outputs... and this is clearly difficult to keep track of. As an alternative you might like to consider using a structure, which allows you to do this:
function out = temp(x)
out.data = x;
out.sqrt = sqrt(x);
end
and then we can call the function and get all of the output values in just one variable:
>> X = temp(4)
X =
data: 4
sqrt: 2
This has many advantages over trying to pass lots of output variables! You can also use the same method for passing input variables :)
Julia
am 11 Mai 2015
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!