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

3 次查看(过去 30 天)
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 个评论
Mohammad Sami
Mohammad Sami 2024-7-8
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.

请先登录,再进行评论。

采纳的回答

Roop
Roop 2024-7-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by