MATLAB Answers

Preallocation with object structures

4 views (last 30 days)
Diego Hens
Diego Hens on 3 Sep 2020
Commented: J. Alex Lee on 4 Sep 2020
Hello,
I have a question about preallocation with object-oriented programming.
I have this object:
This is the result of a few loops. I could have one only loop, but I prefere to do it step by step to better control that I'm not wasting my time with dumb errors. I think it's not important, but I'll show an example.
This is an object containing the tooth 11 (FDI nomenclature) of six different samples (STL-files) of teeth.
First I rotate and translate a tooth to fit a reference tooth (in this case tooth in second row). For this I get tform (transformation matrix) and movingReg (new set of points/vertices).
Then I do a nearest point analysis and get k and dist an calculate Fk which is the distance vector with which I calculate the MeanVertices for the reference tooth (that's why there is only one matrix, in the second row).
Matlab tells me to preallocate the whole time, which I don't know how to do with this object. How can I preallocate (for example) FDINumber11.Vertices (see the code bellow this paragraph) before reading the data and not knowing how many points the file has? I think solution would apply to the other characteristics.
for i=1:m % m is the amount of teeth, which is defined at the beginning
FDINumber11_STL = stlread(FDINumber11(i).STLPath);
FDINumber11(i).Vertices= FDINumber11_STL.Points;
FDINumber11(i).Faces = FDINumber11_STL.ConnectivityList;
end
The whole process takes a little while, not too much but I hope to one day try it with many more teeth, so optimization would be a good thing to do. The worst case is that I learn a good programming practice, so even in case I cannot preallocate here, I would be glad to get a good explanation of it.
As always thank you very much.
Diego
PS: I have an example of a preallocation here, used in this code and it seems to work.
summe1 = 0.0;
for i=1:m
summe1 = summe1 + (FDINumber11(i).Fk - FDINumber11(r).Vertices)/(m-1);
end

  0 Comments

Sign in to comment.

Accepted Answer

J. Alex Lee
J. Alex Lee on 3 Sep 2020
I don't think you are understanding "preallocation" correctly based on your last example...matlab is [probably] asking you to "reserve space fo the m different stl files you want to process". In this case, m=6 if I understand your description. There is a simple fix:
for i = m:-1:1
FDINumber11_STL = stlread(FDINumber11(i).STLPath);
FDINumber11(i).Vertices= FDINumber11_STL.Points;
FDINumber11(i).Faces = FDINumber11_STL.ConnectivityList;
end
What this does is instead of go i = 1,2,3,4,5,6 it will go i = 6,5,4,3,2,1. This matters because
clear % clear all variables
x(6) = 1
is a way to implicitly tell matlab to preallocate 6 elements to the 1-D array x before assigning the 6th element. It works for structure arrays too.
It's also likely that you want to enapsulate all the analysis you do on an individual stl file into a single function, rather than do things in a loop with indices.
function results = stlAnalysis(stlpath)
% do stuff
end

  6 Comments

Show 3 older comments
J. Alex Lee
J. Alex Lee on 4 Sep 2020
My point is, based on your first bit of code I think matlab wants you to tell it how many stl files you have (m=6), not how big your stl parsing results are. Simply reversing your loop order should resolve your mlint warning (literally replace your first bit of code with my first bit of code). I don't think it will affect performance much. If you are having different preallocation warnings, please show the code that's generating them
Diego Hens
Diego Hens on 4 Sep 2020
Yes, that I have understood. When I replaced my code with a function I did not get the warning. I was just asking about the general use of preallocation. So, when I want to do a preallocation, I need to know the size of the matrix, right? In this case there is no preallocation, just the order of the loop.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!