In openscad I need to extract a value from an array without it being it's own array. For example I have an array of values
a = [0,1,2,3,4];
When I assign a[0] to a variable the variable echos [0] showing that the variable is a single celled array.
For example:
b = a[0];
b = [0]; instead of just 0. How can I make b = 0?
Related
I've got a nested struct array like
A(1).B(1).var1 = 1;
A(1).B(2).var1 = 2;
Now I want to change the values of var1 to using the elements of the vector x = [3; 4] for each of the respective values.
The result should be
A(1).B(1).var1 = 3;
A(1).B(2).var1 = 4;
I have tried
% Error : Scalar structure required for this assignment.
A(1).B.var1 = x;
% Error : Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
[A(1).B.var1] = x(:);
Curiously, if x is a cell array, the second syntax works
x = {3, 4};
[A(1).B.var1] = x{:};
Luckily, it's not too complicated to convert my numeric vector to a cell array using mat2cell, but is that the only way to do this assignment without a for loop?
What's the correct syntax for multiple assignment to a nested struct array? Can I use numeric vectors or do I have to use cell arrays?
The statement
[A(1).B.var1] = x{:};
is shorthand for
[A(1).B.var1] = deal(x{:});
(see the documentation for deal).
Thus you can also write
[A(1).B.var1] = deal(3,4);
I'm not aware of any other way to assign different values to a field in a struct array in a single command.
If your values are in a numeric array, you can easily convert it to a cell array using num2cell (which is simpler than the mat2cell you found).
data = [3,4];
tmp = num2cell(data);
[A(1).B.var1] = tmp{:};
In general, struct arrays are rather awkward to use for cases like this. If you can, I would recommend that you store your data in normal numeric arrays, which make it easier to manipulate many elements at the same time. If you insist on using a struct array (which is convenient for certain situations), simply use a for loop:
data = [3,4];
for ii = 1:length(A(1).B)
A(1).B(ii).var1 = data(ii);
end
The other alternative is to use table.
I want to get the size of a certain field inside a structure.
For example the size of the field Name inside the field Dimensions of the struct obtained from ncinfo:
finfo = ncinfo('example.nc');
finfo.Dimensions.Name
>>ans =
x
ans =
y
ans =
z
Just using size causes an obvious error:
size(finfo.Dimensions.Name)
Error using size
Too many input arguments.
How can I do it in an alternative way?
Also, I would like to save the content of finfo.Dimensions.Name in a separate array or struct. But I get a similar error. For example:
a.b=finfo.Dimensions.Name
returns the error:
Illegal right hand side in assignment. Too many elements.
Per the documentation for ncinfo, Dimensions is an array of structures, so you need to be more explicit with what you want to do.
If you want the size of the 'Dimensions' field then that is your query:
S.Dimensions(1).Name = 'x';
S.Dimensions(2).Name = 'y';
S.Dimensions(3).Name = 'z';
size(S.Dimensions)
Which returns:
ans =
1 3
Your problem is that the Dimensions field in the structure returned by ncinfo is itself an array of structures, and when you access a field of a structure array it returns a comma-separated list of values, one for each array element. You need to collect these values, for example in a cell array:
nameCell = {finfo.Dimensions.Name}; % Now a 1-by-3 cell array of names
If you just want to know the number of dimensions, you can check the size of the Dimensions field like so:
N = size(finfo.Dimensions);
I have matlab data 1x617 stuct with 1 field and each row has 1x1 struct with 4 fields. How can I extract the data to get the 4th field for 617 data, the 4th field is nx1 double. Thanks
Depending on your variable structure, it can be sometimes easily solved by simply index reference it. However, in your case that you want to collect data from one specific field within array of struct variable, I would recommend to use structval on Matlab FileExchange
Here is the link:
Struct 2 array or cell for desiginated fieldname
Let's assume your variable looks like this
for idx = 1:617
b.a = ones(1,1)*idx;
b.b = ones(2,1)*idx;
b.c = ones(3,1)*idx;
b.d = ones(4,1)*idx;
s(1,idx).a = b;
end
The above code creates a 1x617 stuct with 1 field, and each row has 1x1 struct with 4 fields. And here I assume that the name of the 4th field in each row is the same.
Now use structval
r = structval(s,'d');
The result is a 1x617 cell array. If you happen to have same size for that field at each index (e.g. a nx1 array), then use
r = structval(s,'d','collapse',true);
to return a nx617 array
How to create an empty array in matlab that accepts elements from a matrix when you do not know the no.of elements it is going to contain ?
Use the [] operator. Example:
x = [];
If you wanna be specific in the type of the empty matrix, use the empty property. Examples:
emptyDoubleMatrix = double.empty; % Same as emptyDoubleMatrix = [];
emptySingleMatrix = single.empty;
emptyUnsignedInt8Matrix = uint8.empty;
This works for empty matrices of classes as well. Example:
emptyFunctionHandleMatrix = function_handle.empty;
You can use the empty matrix/vector notation, [], and Matlab will set up a placeholder for it.
x = []
Now if you want to append a scalar, say num, to it, you cannot index it because it is empty.
However, you can either:
Use array concatenation to concatenate itself with another scalar:
x = [x num]
Use the end+1 notation, to address the first available location:
x(end+1) = num
Both of the above two notations also work when you want to append a row or a column vector to an existing row vector or column vectors. But when you are concatenating vectors/matrices, remember to be consistent with the dimensions.
There has a loop in my program, and during each iteration an ID will be generated. I want to store these IDs into a two dimensional array, i.e., A. The first column of A stores the iteration number, i.e., A(1,1) = 1 and A(2,1) = 2. The second column of A stores the ID generated during each iteration, i.e., A(1,2) stores the ID generated during the first iteration. The tricky part is that these IDs can be either a numerical value or a string. For instance, A(1,2) = 12345; A(2,2) = abcde
Which kind of data structure should I use to store this mixed-value matrix?
You have two good options, a cell array or an array of structures.
To use a cell array you need to use braces:
A{1,1} = 1;
A{2,1} = 2;
A{1,2} = 12345;
A{2,2} = 'abcd';
You cannot use most vectorized code with cell arrays, although you can convert numeric subsets to numeric arrays, for example:
col1 = cell2mat(A(:,1));
To use an array of structures, you need to define fields. This has the advantage that you can name your columns of data.
A(1).iteration = 1;
A(2).iteration = 2;
A(1).result = 12345;
A(2).result = 'abcd';
To access a single row of data, use A(1), like this
>> A(1)
ans =
iteration: 1
result: 12345
To access a column of data, use brackets or braces
>> [A.iteration] %This results a numeric array, or an error if not possible
ans =
1 2
>> {A.result} %This returns a cell array, as discussed above.
ans =
[12345] 'abcd'
Which option you use depends on the nature of your task and what method is more suitable to your style. I usually start with a cell array, and eventually convert to an array of structs to take advantage of the named fields.