Working with objects from other classes within MATLAB app editor
21 次查看(过去 30 天)
显示 更早的评论
Hello,
I am just getting started simultaneosly with OOP and app editor and I have an issue handling with data objects defined under another class (separete class definition in a .m file). This is the class definition I use to arrange the raw data, it is pretty simple:
classdef Land < handle
properties ( Access = ?GlobalCovidData)
Name
Main
CovidCases
DeathCases
Regions
end
%include methods to set the Names as strings of the corresponding cells
end
As you see, the properties of the class are open to GlobalCovidData, which is my app. the Now, within the app editor I wrote a huge code to arrange the data in objects under the class Land. This code is writen within the function startupFcn(app) and allows to draw the data and plot them. Within the startupFcn(app) there are several definitions of data objects for the class Land. These are just random code lines to show that:
%when drawing data from the given file
world = Land; world.Name="Global";
countries{count} = Land; countries{count}.Name=string(A{i,1}); countries{count}.Main = world;
divisions{i-count-1} = Land;
% these ojects are basically covid19 incidence data of all countries
for k=2:length(countries)
% add next country to world cases
world.CovidCases = world.CovidCases + countries(k).CovidCases;
world.DeathCases = world.DeathCases + countries(k).DeathCases;
end
%when creating arrays to plot, that already worked
app.Positives = world.CovidCases;
app.Deaths = world.DeathCases;
Now when I try to use the same objects from callback function of a widget, I get an error upon running the app:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/479543/image.png)
Note that countries and world had alread worked within the startupFcn(app) or its methods. I already defined the Land class as property of the app class. I think I can try writing the function to handle with this objects withing the app methods, but then I don't understand much what are the callback functions for. Are they only as event triggering, that means, to call class (app) properties?
Thanks in advance for any clue.
BR,
Carlos.
0 个评论
采纳的回答
Steven Lord
2021-1-5
The countries variable was created within the workspace of that startupFcn function call. Since it was not returned as an output from startupFcn, it was destroyed along with the workspace when the function call ended.
Since this is in an app you probably want to store that variable in a property of your app object. If you do that, you could access it via that object inside the callback function. See this documentation page for an example.
3 个评论
Steven Lord
2021-1-6
I tried to include all variables as app properties and stored the data in that objects. But I get the same error message. Unrecognized function or variable 'Country'.
Show us a small segment of your modified code. If you set Country to be a property of the app class:
classdef myapp
properties
Countries
end
end
and you try to use it but as a variable that won't work. You would need to use it as a property:
function setup(app)
y = Countries; % will not work
end
function setup(app)
y = app.Countries; % works if Countries is a property of app
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!