フィルターのクリア

Passing a component as an argument in App Designer

11 ビュー (過去 30 日間)
Luke Somers
Luke Somers 2018 年 7 月 1 日
コメント済み: Ameer Hamza 2018 年 7 月 2 日
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 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 7 月 1 日
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.
Luke Somers
Luke Somers 2018 年 7 月 1 日
編集済み: Luke Somers 2018 年 7 月 1 日
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.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 7 月 1 日
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 件のコメント
Luke Somers
Luke Somers 2018 年 7 月 1 日
編集済み: Luke Somers 2018 年 7 月 1 日
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.
Ameer Hamza
Ameer Hamza 2018 年 7 月 2 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by