Working with objects from other classes within MATLAB app editor

16 次查看(过去 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:
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.

采纳的回答

Steven Lord
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
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
Carlos Rueda
Carlos Rueda 2021-1-7
Hello Steven,
thanks again. After messing too much with my code I worked it out. Obviously, I cannot try to instantiate properties of a class as objects of another class, I just need to store the objects of that other class (Land) in arrays that are properties the app class GlobalCovidData.
Maybe I can try to clear such stuff up before posting a question next time :).
BR,
Carlos.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by