MATLAB: App Designer: How to use input arguments from startupFcn in the section Properties

app designerimage processingMATLABvariables

I'm new to MATLAB's App Designer. I'm trying to make an app that reads in a 3D matrix and shows axial, coronal and sagittal views. However, early steps have already proven to be difficult.
I'm using the startupFcn to give the 3D matrix as an input (function startupFcn(app, Img)). Then, I want to calculate some basic information from this input matrix (e.g. size) in the properties section to use in other callbackfunctions (see below). This results in the following error:
"Invalid default value for property 'size_Img' in class 'BoundingBox': Unable to resolve the name app.Img."
I have tried setting the input variable Img global and using the access property to public (as seen below), but I keep getting the same error. Am I doing something wrong?
Thanks in advance for the pointers!
Sam
properties (Access = private)
size_Img = size(app.Img) % Image Size
size_Sag = size_Img(1); % Image Saggital Size
size_Trans = size_Img(2); % Image Transverse Size
size_Front = size(3); % Image Frontal Size
...
end
properties (Access = public)
Img % Input image
end

Best Answer

Hi Sam,
From my understanding of the question, the error is being produced because you are trying to set the properties when the component is being created (This works correctly only if the value to on RHS is constant (e.g. size_x = 2 will work but size_x = size(X) won't work). The component creation step happens before calling the startup function. At the component creation time, Img was not defined, and hence it is throwing error.
Instead of initializing the properties values at declaration time, you should set them when startup function is called.
Following is the code that you will find helpful.
properties (Access = private)
size_Img % Image Size

size_Sag % Image Saggital Size

size_Trans % Image Transverse Size

size_Front % Image Frontal Size

...
end
properties (Access = public)
Img % Input Image
end
function startupFcn(app)
app.size_Img = size(app.Img); % Image Size
app.size_Sag = app.size_Img(1); % Image Saggital Size
app.size_Trans = app.size_Img(2); % Image Transverse Size
app.size_Front = size(3); % Image Frontal Size
end
For understanding in detail, you can refer the following links
  1. https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
  2. https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html