MATLAB Answers


Passing a component as an argument in App Designer

Asked by Luke Somers on 1 Jul 2018
Latest activity Commented on by Ameer Hamza
on 2 Jul 2018
I am working in App Designer, and I have 3 UIAxes objects, and I want to plot an image to each of them. Rather than duplicate code, I created a function taking the app and a UIAxes object: "function assignSub(app, axes)" In this function, I do some things to prepare the image and the axes, and then I call "image(axes, myImage);"
Only, I get an error: "Specify a UIAxes handle as first argument"
Now, I know, Matlab is weakly typed, but is there some way for me to reassure the compiler that axes is in fact a UIAxes handle? Or can I get it to just trust me on this? Or is it somehow NOT a UIAxes handle?
About that - the call for this function looks like "app.assignSub(app, app.subImage1);" for a variety of subImages. I tried changing "app.subImage1" to "@app.subImage1" which is kind of how you turn a function into a function handle, but that didn't fix it. I wouldn't be surprised if that isn't valid code at all.
If all else fails, I guess I can do the work of preparing the image and axes inside the function, and then have it return myImage, and only then do I call "image(app.subImage1, myImage);". Then the compiler doesn't need to realize that the UIaxes are UIaxes while they're in the function, only when they're accessed directly from app. But this would require me to duplicate code for each time I do this, rather than just having one function call for each.


Why do you need to separately pass axes to assignSub. As app.subImage1 shows that subImage1 is a property of app object, so just use app object in assignSub. For example
image(app.subImage1, myImage);
Also, are you sure that app.subImage1 is a UIAxes object? You should use MATLAB debugger breakpoint to pause the code at this line and find the class if axes.
But then the function would only work on that one axes object, which completely nullifies the whole point of making a function. I want to be able to aim it at one, and then another, and then another, and so on, each of the targets being an axes object within the same app.

Sign in to comment.




1 Answer

Answer by Ameer Hamza
on 1 Jul 2018
 Accepted Answer

I have attached an example app, in which I have three axes and I have written a simple function which plots random values on input axes. The syntax you initially mentioned should be working. If you received "Specify a UIAxes handle as first argument", then it is actually just a warning. At runtime, MATLAB will decide the class of the function variables based on input data. Also if the assignSub is written inside the app then you may need to use it like this
first app input is not required. MATLAB will automatically pass the class object as the first argument whenever a non-static class method is called.


Thanks! About the arguments, I was trying various things as I wrote the question, and ended up using inconsistent signatures in my explanation here in the process. Good to know that some of these warnings are things that can be brushed off.
You are welcome.

Sign in to comment.