Calling a function defined in a m-file from within appdesigner app, with the app object as function input

4 次查看(过去 30 天)
I'm using R2019b.
I have created a GUI app using appdesigner and have written the function testFun in a m-file (testFun should also be usable by a non GUI approach, hence the m-file).
Within the app, I have declared a public property called someData:
properties (Access = public)
someData = '';
end
From within the GUI app, testFun is called with app as a function argument:
testFun(app)
testFun is defined as follows:
function testFun(inputData)
inputData.someData = 'hello world';
end
As you can see, there are no output arguments defined in testFun.
Yet when I run the app like this, the someData property gets the value 'hello world'.
How can this behavior be explained?

采纳的回答

Rik
Rik 2019-12-20
编辑:Rik 2019-12-20
Your GUI (like all AppDesigner GUIs) has a pass-by-reference behavior (so it behaves like a handle class). That means the line you show modifies the original object, instead of creating a new one when the function is called.
You could create a GUI that doesn't work this way by using the normal figure class, so what is generally meant by a 'programmatic GUI', or GUIs created with GUIDE. For an overview of your options for GUI design, see this. For more information about pass-by-reference vs pass-by-value, see Comparison of handle and value classes in the documentation.
  8 个评论
Lucademicus
Lucademicus 2019-12-20
Alternatively I could create a little shell script which unzips myGUI.mlapp to myGUI/ everytime the mlapp-file is modified. This fits more readily in my Git workflow. Then I can keep the myGUI/ folder and the mlapp-file both within versioning. Any thoughts on this?
Rik
Rik 2019-12-20
I don't know what the designer code equivalent would be in Matlab, but you could make a programmatic GUI based on a class instead of functions. That way you would have a text file for Git to work with.
But I'm not sure if that actually solves the issue, since you leave the AppDesigner to do this.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Source Control 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by