Error with calling a method from one app into another

Hello, I am trying to call methods/functions from another app and have read you do it like this, but its not working.
1: Create the app and define the methods in the public area,
methods (Access = public)
function myTestMessage(app,msg)
disp(msg)
end
end
This is then saved as myFits.mlapp
Then in another app, I attempt to call this external method by a pushbutton call:
% Button pushed function: TestingButton
function TestingButtonPushed(app, event)
%Call external App methods form myFits app.
myTestMessage(myFits,'Test message passed to external app')
end
But I get the error:
Unrecognized function or variable 'myFits'.

8 件のコメント

Mohammad Sami
Mohammad Sami 2020 年 1 月 28 日
編集済み: Mohammad Sami 2020 年 1 月 28 日
Before you can access the method of the first app, you need to create an instance of the first app.
You can store the handle of the app as a property of your second app.
app2.myFits = myFits();
app2.myFits.myTestMessage('Test message passed to external app');
Then you can access its public method.
Jason
Jason 2020 年 1 月 28 日
Ah OK, Im not finding anything whislt searching both matlab and google
Jason
Jason 2020 年 1 月 28 日
Sorry, but for the app2.myFits=myFits, this goes into the calling app?
If so, Im still getting an erro:
function TestingButtonPushed(app, event)
%Call external App methods.
app.myFits = myFits();
app.myFits.myTestMessage('Test message passed to external app')
Mohammad Sami
Mohammad Sami 2020 年 1 月 28 日
I am assuming that your first app is called myFits() and is saved in the same folder as your second app.
The file would be called myFits.mlapp . If its called something else, you should change the code appropriately.
Jason
Jason 2020 年 1 月 28 日
Ah, shoot, its not in the same directory, doe it have to be. I was hoping it could be a generic directory so could be picked up by many apps?
Jason
Jason 2020 年 1 月 28 日
OK, moved it into the same directory as my main app (called spectrometer.mlapp), but now get this
Unrecognized property 'myFits' for class 'Spectrometer'.
Error in Spectrometer/TestingButtonPushed (line 1968)
app.myFits = myFits();
Adam
Adam 2020 年 1 月 28 日
編集済み: Adam 2020 年 1 月 28 日
You have to have an actual object of the class you are trying to call the function on (since it is not a static method), and this object must be available in the scope you call it.
Where the mlapp is located in your file structure is irrelevant so long as it is on your path.
If you can create an object of myFits then it is fine wherever it is. You then have to give this object to your other app, in some way or other so that in the TestingButtonPushed function you have access to this object. Then you can call its public methods or access its public variables.
In terms of the last error, you have to define myFits in a properties block of your class if you are going to use
app.myFits = ...
Jason
Jason 2020 年 1 月 28 日
Perfect, thankyou

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDevelop Apps Using App Designer についてさらに検索

質問済み:

2020 年 1 月 28 日

コメント済み:

2020 年 1 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by