Working with objects from other classes within MATLAB app editor

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 日

0 投票

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 件のコメント

Carlos Rueda
Carlos Rueda 2021 年 1 月 5 日
Hello Steven,
thanks for your response. 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'. And I used this syntax to treat that variable as object of the my class Land:
y = app.X % Get the value of X
app.X = 5; % Set the value of X
But it doesn't work. Now I guess this is either a matter of syntax or definition of a new constructor to allow the app to create and store objects?
Since my assigment specifies following:
"The data is available from the attached .mat file attached below. Once you load it, you will get a single variable called that is a large cell array. (Make sure your app loads the file!)"
Your program must convert this data into a set of objects: one object per country and state. States should be contained by their countries. Countries could be stored in a vector of country objects in the app itself. Another way is to create an instance of the same class you use for countries and states, call it global, and have it store all the countries. The app would then contain the single global object as a property. This option would create a 3-level hierarchy: the global object stores data for the entire world and a vector of country objects, while the objects of countries that have states in the database would store their corresponding states. Again, you can use the same class definition for all three kinds of objects because they store essentially the same kind of data."
... I don't really know how else I can deal with that. How can I approach that? The properties of my class Land are not being recognized and I cannot define a new class within the app class.
I must use a class outside the app because:
"Submit all MATLAB files necessary to run your program in a single zip file. Typically, you would have three files: the app file (.mlapp), the m file with the class definition of your country/state objects and the .mat file with the data."
Since I started creating the data objects first in a MATLAB to get started with objects and it worked, I am ver confused now and I don't find within the documentation a similar issue.
I would be very thankful if you could give me one more hint.
BR,
Carlos.
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 件)

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by