Reputation: 4807
I have a function which is supposed to return two categories of data 'City' and 'Fruits'. For each category based on input data there would be varying number of arrays for various cities e.g. NYC, DC, Arlington, etc. So, sometimes the data may make user return 3 cities other time it might make him return 5. Similarly for category 'Fruit' there might be varying number of returned arrays.
I don't know whether my explanation above makes sense but here is some pictorial representation:
Category: Cities
Table1 Name: NYC
Table1 Data:
1 4
2 5
4 6
6 7
Table2 Name: DC
Table2 Data:
11 41
25 5
48 65
61 70
Similar is the structure for category Fruits.
The function is supposed to return all the values in one piece. How do I achieve it ?
Upvotes: 1
Views: 87
Reputation: 124563
How about a structure array:
cities(2) = struct('name','', 'data',[]);
cities(1).name = 'NYC';
cities(1).data = rand(4,2);
cities(2).name = 'DC';
cities(2).data = randn(5,2);
The result:
>> cities
cities =
1x2 struct array with fields:
name
data
>> cities(1)
ans =
name: 'NYC'
data: [4x2 double]
>> cities(2)
ans =
name: 'DC'
data: [5x2 double]
Similarly for fruits
.
Upvotes: 2