Passing objects from app designer to the workspace

Hello , I'm using app designer and I am creating object when some button clicked, (in the callback function) i'm trying to use the object that created in the callback funcion in the workspace. I know that i can create some "global" object in the properties and then use app.obj. There is other way that i can pass the object from the callbackfunction to the workspace? or the only way is to use the properties.
Thank you very much :)

 Accepted Answer

Though I would recommend using one of @Adam Danz's solutions since they are typically a safer method of passing data, assignin can be used to programmatically assign a variable to the workspace.
See the following help page for details on how to use the assignin function.

6 Comments

Ron
Ron on 23 Oct 2022
Edited: Ron on 23 Oct 2022
Thank you very much exactly what i were looking for !
Can you understand why @Adam Danz solution better? i mean why you are saying his solution is in safer method
Hey again !
Well, i understand that assignin isn't good way to use for my solution , but there is any another way that the "button callback function" can return a value? I am trying to use obj in some matlab file after click button pushed but my variable doesn't pass to the workspace( because after button pushed the functions ends and the)
There is any way to change that when button pushed i can return value?
Ron,
How have you setup your application? For example, are you using GUIDE, AppDesigner, or have you created a UIFigure with additional UI Objects such as the push-button? Also, could you provide any relavent code for your push-button callback function identifying the variable that you are wanting to replicate within the workspace? This will help others to provide more meaningful solutions to your questions.
I am using appDesigner,
Function RunButtonPushed(app,event) //NOTE : this is on gray cant add a return value
obj=importdata(somepath) //NOTE : The path is matlab data that have run method
obj.run() // NOTE : after the run , nothing happens because the variable
end obj is uknown in the workspace i guess beacuse in the
end of this function there is END
Ron,
When working with callback functions in AppDesigner use the app object that is called in as the first input argument to store additional user defined values. To do that you will need to setup a new property in a user defined properties block. Typically, I will create at least a basic propery arbitrarily named Data similar to the following. This will store the imported data within the application and can be accessed at any time. If you are familiar with using GUIDE, you will find that this works similarly to behavior of the guidata function, but is a more simplified process.
classdef UserAppClass < matlab.apps.AppBase
properties (Dependent, SetAccess = public)
...
Data
....
end
...
% Additional method blocks containing user defined functions and app callback functions
...
end
With a dedicated property setup you can then store and retain the imported data similarly.
% Callback function assigned to the import data pushbutton
function RunButtonPushed(app, event)
app.Data = importdata(somepath);
end
% Callback function assigned to an demonstrational plot data button
function PlotDataButtonPushed(app, event)
% Callback function stops immediately if the Data property is empty
if isempty(app.Data)
return
end
figure
% Assumes data was a numeric double-array with at least two columns
plot(app.Data(:,1),app.Data(:,2))
end

Sign in to comment.

More Answers (1)

The best way to access the object created by an app is to store the handle to the object as a public property of the app and to access that handle through the app object, stored as a variable in your workspace.
More info

Categories

Find more on Code Execution in Help Center and File Exchange

Products

Release

R2022a

Asked:

Ron
on 18 Oct 2022

Commented:

on 17 May 2023

Community Treasure Hunt

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

Start Hunting!