Working with objects from other classes within MATLAB app editor

28 vues (au cours des 30 derniers jours)
Carlos Rueda
Carlos Rueda le 5 Jan 2021
Commenté : Carlos Rueda le 7 Jan 2021
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.

Réponse acceptée

Steven Lord
Steven Lord le 5 Jan 2021
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 commentaires
Steven Lord
Steven Lord le 6 Jan 2021
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 le 7 Jan 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by