how to get array of properties from array of objects in matlab - arrays

I am using an array of objects in my program, and each object has several attributes. I want to be able to extract separate arrays from this array of objects, one array for each attribute.
here is a snippet of the relevant code:
dailyDataMatrix(m,n)=dailyData('',''); %creates an mxn array of dailyData objects
for i=1:m
for j=1:n
dailyDataMatrix(i,j)=dailyData(datainput1, datainput2)%dailyData constructor, sets attributes
end
end
dailyDataMatrix.attribute
But when I try to access a certain attribute as in the code above, say of type string, I get a strange result. Instead of getting an array of strings, I get something else. When I try to print it, rather than printing an array, it prints a series of individual values
ans = 'string1'
ans = 'string2'
...
When I try to call
size(dailyDataMatrix.attribute)
className = class(dailyDataMatrix.attribute)
I get
"error using size: too many input arguments" and
"error using class: The CLASS function must be called from a class constructor."
However, when I write this as
thing=dailyDataMatrix.attribute
className = class(thing)
size(thing)
I get the response
classname = 'double' and size = 1x1.
Why is this not returning an array the same size as dailyDataMatrix? And an aside question is why the two different ways of writing the code above give different results? and how can I get the result that I want?
Thanks,
Paul

You can capture all the outputs using a cell array or using square brackets if the types are same. For regular array when all values are of same type use,
thing=[dailyDataMatrix.attribute];
If the types are different you can use
thing = cell(1,N); % N is the number of elements in dailyDataMatrix
[thing{:}] = dailyDataMatrix.attribute;

Related

Matlab: Unable to perform assignment because the left and right sides have a different number of elements

labs = 1:1334;
for i = 1:1334
labs(i) = num2str(i,'%06.f');
end
I want to put "000001" "000002" etc as strings into the "labs" array. However, it says each side has a different number of elements. Why don't they both only have one?
The issue stems from the fact that labs is defined as a double array by default, meaning that its elements are expected to be numeric. In the for loop, you're trying to assign string values (using the num2str function) to the labs array which is of the class double. A workaround would be to define labs as a cell array, which can store string values.
N = 1334;
labs = cell(N,1);
for i = 1:N
labs{i} = num2str(i,'%06.f');
end
The only caveat is that labs is now a cell array, which requires a different syntax than regular double arrays to index its elements.

How to show elements of array?

I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]

MATLAB: Object Array Property Assignment with Deal

I have a class constructor that takes multiple numeric arrays of the same size and creates an object array of the same size. In the constructor, I am trying to use deal to assign data from each input array to a single index of the object array's only property:
classdef myClass
properties
data
end
methods
function obj = myClass(varargin)
nArg = length(varargin);
for iArg = 1:nArg
dataArg = num2cell(varargin{iArg});
[obj.data(iArg)] = deal(dataArg{:});
end
end
end
end
Unfortunately, I get the following error from deal: The number of outputs should match the number of inputs.
I believe that this is probably occurring because I haven't preallocated the object array before the for loop in the constructor, e.g.:
szObj = size(varargin{1});
cellSzObj = num2cell(szObj);
obj(cellSzObj{:}) = myClass.empty;
However, for various reasons, I want to be able to make this assignment without preallocating the object array.
Is there a way to do this?

Matlab - Extract properties as array from object-array

how do I extract an array of object properties from an object array (where each of the objects within that array has that property?)
Ex.:
classdef myClass
properties
myProperty = 1
end
end
--
myObjectMatrix(1:1000) = myClass()
myObjectMatrix(100:234).myProperty % what I thought would work but results in lots of individual results
[myObjectMatrix(100:234)..myProperty] works, but only in one dimension. I need to use reshape() if I have more than one dimension to 'fold' my results back.
Is there a better way?
Thanks!
Basically that code would act on each member in turn and return a separate answer, so in the end you only get a 1x1 output.
The solution in that example is to use arrayfun(), such as:
myObjectMatrix(1:1000) = myClass()
output = arrayfun(#(x) x.myProperty,myObjectMatrix(100:234))
That would give you a 1x135 array containing the value of each of the myProperty member from each of the elements selected from the class array.
In arrayfun you give a function to perform on each element in the array and then the array to act on. In this case I created an anonymous function which simply accesses myProperty on x - where x will be each object in the array in turn.
It is important to note that the above will only work if the property is a single value, not a matrix/array. If it is an array then the output will be nonuniform, and you will have to do:
output = arrayfun(#(x) x.myProperty,myObjectMatrix(100:234),'UniformOutput', false)
In this case 'output' will be a cell array containing the value of the property of each class.

Initialize Array to Blank custom type OCAML

ive set up a custom data type
type vector = {a:float;b:float};
and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.
the following
let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}
makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible?
You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program caused by uninitialized values.
If the values you eventually want to place in your array are not available yet, maybe you are creating the array too early? Consider using Array.init to create it at the exact moment the necessary inputs are available, without having to create it earlier and leaving it temporarily uninitialized.
The function Array.init takes in argument a function that it uses to compute the initial value of each cell.
How can you have nothing? When you retrieve an element of the newly-initialized array, you must get something, right? What do you expect to get?
If you want to be able to express the ability of a value to be either invalid or some value of some type, then you could use the option type, whose values are either None, or Some value:
let vecarr : vector option array = Array.create !max_seq_length None
match vecarr.(42) with
None -> doSomething
| Some x -> doSomethingElse
You can initialize and 'a array by using an empty array, i.e., [||]. Executing:
let a = [||];;
evaluates to:
val a : 'a array = [||]
which you can then append to. It has length 0, so you can't set anything, but for academic purposes, this can be helpful.

Resources