How to pass an object of handle class to the function constructing the app in App Designer?

3 views (last 30 days)
In my App Designer, the default function that constructs the app takes varargin as input. The function creates UIFigure and Components, registers the app with AppDesigner, and executes the startup function. Say this is represented by "B" (a matlab function with same name as mlapp file created by AppDesigner automatically and cannot be edited because greyed out).
The object of handle class essentially creates the object, creates the structs to store data, and initializes the structs with default/initial data. Say this is represented by "A" (an object of handle class).
I am getting an error when doing:
var_name = B(A);
Error Code:
"Cannot pass an object of handle class 'A' to MATLAB function 'B'"
  1 Comment
Mohammad Sami
Mohammad Sami on 8 Jul 2024
you need to add a startup function to you app designer app. matlab will then call this startup function with the arguments that are passed in to your app. you can then write your code in the start up function to do something with the object A.
more information is available here in the help file.

Sign in to comment.

Accepted Answer

Roop
Roop on 9 Jul 2024
When using coder.extrinsic, functions declared as extrinsic will return mxArray types. If you pass these to other Matlab functions, Coder will resolve everything well, but if you use them with your own functions or in any way try to manipulate them, you need to help Coder resolve them into a known type. You do this by pre-defining a variable and copying the mxArray into it, which will allow Coder to correctly convert to a standard data type. If you know the exact size beforehand, you can preallocate the variable before the call and skip the copy, but in this case it may be a bit trickier.
In the case of your function, I assume you have a call somewhere that looks like:
I = imread([some paramaters]);
We need to get the mxArray type from the call to imread, then determine its dimensions so that another variable can be allocated in a native type. The determination of the mxArray dimensions using the sizefunction itself needs to have a preallocated variable so that size does not return a mxArray also. Here are the steps:
coder.extrinsic('imread');
Itemp = imread([some paramaters]);
idims = zeros(1,3); %Need to preallocate idims so it does not become an mxArray
idims = size(Itemp)
I = coder.nullcopy(zeros(idims, 'uint8')); % Allocate but do not initialize an array of uint8 values of the same size as Itemp
I = Itemp; % Copy the data from the mxArray into the final variable.
If you know the exact size of the image before the call to imread, you can skip the copy and the second variable and simply preallocate the variable I to the correct size, but this is not typically the case for an image read.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!