Passing a component as an argument in App Designer
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luke Somers
am 1 Jul. 2018
Kommentiert: Ameer Hamza
am 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.
2 Kommentare
Ameer Hamza
am 1 Jul. 2018
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.
Akzeptierte Antwort
Ameer Hamza
am 1 Jul. 2018
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
app.assignSub(app.subImage1);
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.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!