Conversion of cell array into individual object

I have 324*231 cell array M.
  1. M(2:end,1) includes names of countries
  2. M(2:end,2) includes names of respective states
  3. M(2:end,3:end) includes some data ( each in {1*2 double} ).
I want to make individual object of each country which contain their states which contain their respective data. So, sort of three level hierarchy.How should I initiate?
Thanks.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 11 Sep 2020
Modifié(e) : Ameer Hamza le 11 Sep 2020
What about using a struct to save data hierarchically. For example
M = [{"C1"; "C1"; "C2"; "C3"; "C3"; "C3"}, ...
{"S1"; "S2"; "S1"; "S1"; "S2"; "S3"}, ...
num2cell(rand(6,3))];
countries = unique([M{:,1}]);
M1 = splitapply(@(x) {x}, M(:,2:end), findgroups([M{:,1}]).');
temp = [num2cell(countries); cell(size(countries))];
S = struct(temp{:});
for i=1:numel(countries)
country_data = M1{i};
for j=1:size(country_data, 1)
S.(countries(i)).(country_data{j,1}) = country_data(j, 2:end);
end
end
C1, C2, and C3 are country names, and S1, S2, and S3 are names of states within a specific country.
Run it like this
>> S.C1.S1 % displays the data for country=C1 and state=S1
ans =
1×3 cell array
{[5.2238e-04]} {[0.8013]} {[0.7386]}

5 commentaires

Yea. I have think same thing but the question forced to do with OOP.
Refernce - 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.
Something like this seems similar to what you described.
Class definition
classdef myObj
properties
name
places % lower level hierarchy
data % data for all places under current object
end
methods
function obj = myObj(names)
if nargin > 0
for i=numel(names):-1:1
obj(i).name = names(i);
end
else
obj.name = '';
end
end
end
end
script
M = [{"C1"; "C1"; "C2"; "C3"; "C3"; "C3"}, ...
{"S1"; "S2"; "S1"; "S1"; "S2"; "S3"}, ...
num2cell(rand(6,3))];
countries = unique([M{:,1}]);
M1 = splitapply(@(x) {x}, M(:,2:end), findgroups([M{:,1}]).');
Global = myObj("World");
Global.data = M(:, 2:end);
Global.places = myObj(countries);
for i=1:numel(countries)
country_data = M1{i};
Global.places(i).data = country_data;
Global.places(i).places = myObj([country_data{:,1}]);
for j=1:size(country_data, 1)
state_data = country_data(j, 2:end);
Global.places(i).places(j).data = state_data;
end
end
Example
Global.places(2) % access 2nd country
Global.places(2).places(1) % access 2nd country 1st state.
Rajan Patel
Rajan Patel le 13 Sep 2020
Modifié(e) : Rajan Patel le 13 Sep 2020
I just want this! but I didn't know about splitapply and findgroups. These built-in functions are amazing. Thank you.Just have one question is it any difference if we use classdef myObj < handle ?
In this case, myObj < handle will have no difference in the output, but it is usually slower. The difference is that handle objects behave somewhat similarly to pointers in C or C++.
Hello, there! This code is just what I needed as well! But I am getting an error. Could you help me? When I run your example, it works fine. But if I run with my cell array (also 324*321) I get the error showed in the image below. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by