Add a number to a struct field [duplicate] - matlab

Suppose I have a struct array arr, where each element has a bunch of fields, including one called val. I'd like to increment each element's val field by some constant amount, like so:
for i = 1:length(arr)
arr(i).val = arr(i).val + 3;
end
This obviously works, but I feel there should be a way to do this in just one line of code (and no for loop). The best I've come up with is two lines and requires a temp variable:
newVals = num2cell([arr.val] + 3);
[arr.val] = deal(newVals{:});
Any ideas? Thanks.

Just a note, the deal isn't necessary there:
[arr.val] = newVals{:}; % achieves the same as deal(newVals{:})
The only other way I know how to do this (without the foor loop) is using arrayfun to iterate over each struct in the array:
% make a struct array
arr = [ struct('val',0,'id',1), struct('val',0,'id',2), struct('val',0,'id',3) ]
% some attempts
[arr.val]=arr.val; % fine
[arr.val]=arr.val+3; % NOT fine :(
% works !
arr2 = arrayfun(#(s) setfield(s,'val',s.val+3),arr)
That last command loops over each struct in arr and returns a new one where s.val has been set to s.val=3.
I think this is actually less efficient than your previous two-liner and the for loop though, because it returns a copy of arr as opposed to operating in-place.
(It's a shame Matlab doesn't support layered indexing like [arr.val]=num2cell([arr.val]+3){:}).

I like Carl's and mathematical.coffee's original ideas.
I have multiple similar lines to express, so for concision of my mainline code,
I went ahead and made the generic subfunction
function varargout = clist(in)
varargout = {in{:}};
end
then I could express each such line in a fairly readable way
[arr.var] = clist(num2cell([arr.var]+3));
[arr.var2] = clist(num2cell([arr2.var]/5+33));

Are all the fields in that struct scalar, or the same size? If so, the idiomatic Matlab way to do this is to rearrange your struct to be a scalar struct with arrays in each of its fields, instead of an array of structs with scalar values in the fields. Then you can do vectorized operations on the fields, like arr.val = arr.val + 3;. See if you can rearrange your data. Doing it this way is much more efficient in both time and memory; that's probably why Matlab doesn't provide convenient syntax for operating over fields of arrays of structs.

if the struct array you are trying to set is a set of graphics objects (line handles, figure handles, axes handles, etc), then you need to use the function set:
x = (1:10)';
Y = rand(10,5);
l = plot(x,Y,'-k'); % returns an array of line handles in l
set(l,'Color','r'); % sets the property 'Color' for all the five lines in l

Related

make iterative variable a cell array in matlab

In matlab, is it possible to make the iterative variable a cell array? is there a workaround? This is the code I ideally want, but throws errors:
dim={};
a=magic(5);
for dim{1}=1:5
for dim{2}=1:5
a(dim{:})=1; %aimed to be equivalent to a(dim{1},dim{2})=1;
end
end
for dim{1}=1:5
↑
Error: Invalid expression. When calling a function or indexing a variable, use
parentheses. Otherwise, check for mismatched delimiters.
I tested that you cannot have A(1), or A{1} or A.x as index variable. https://www.mathworks.com/help/matlab/ref/for.html doesn't explicitly prohibit that, but it doesn't allow it either.
After very slight changes on your code, this should achieve what you seem to want:
dim={};
a = magic(5);
for dim1=1:5
dim{1} = dim1;
for dim2=1:5
dim{2} = dim2;
a(dim{:})=1; %aimed to be equivalent to a(dim{1},dim{2})=1;
end
end
However, I believe the following is a slightly better solution keeping the spirit of "use a cell array to index in your array":
CV = combvec(1:5,1:5); % get all combinations from (1,1) to (5,5). 2x25 double array. This function is a part of deep learning toolbox. Alternatives are available.
CM = num2cell(CV); % 2x25 cell array. Each element is a single number.
for dim = CM
% dim is a 2x1 cell array, eg {2,3}.
a(dim{:}) = 1; % as above.
end
However, none of these are likely a good solution to the underlying problem.

Structure variable as input in MATLAB function using VARARGIN

I wrote a main function in Matlab that calls other functions, each of which produces plots in a new figure. Though each function produces different plots (different colors, number of subplots, and so on) they all share some features (font, fontsize, Linewidth etc.).
In order to make it easier to modify the aforementioned shared parameter for all the MATLAB figures, I have defined in the preamble of the main function a structure variable as follows:
var.font = 'Arial Unicode MS';
var.fontsize = 13;
var.interpreter = 'none' ;
and so on for the other fields. When I call the function in this way (providing the structure as input):
function plot1( var , ... )
fig = gcf
fig.Position(3) = var.Position3
fig.Position(4) = var.Position4
end
everything works fine and the functions apply the feature to each figure. But if I try to provide a variable number of input to the functions using varargin, in this way
function plot1( varargin )
fig = gcf;
temp = varargin(1);
fig.Position(3) = temp.Position3;
fig.Position(4) = temp.Position4;
end
The following error message is prompted "Struct contents reference from a non-struct array object."
You're indexing the cell array incorrectly (this is easily done).
Round parentheses ( ) give you a cell output when indexing a cell array - i.e. your temp is a 1x1 cell with the struct inside it.
You need curly braces { } to extract the contents of a cell array.
Fix: use curly braces:
temp = varargin{1};
I sometimes think of cell arrays as a group of boxes, since each element (or "box" in this analogy) can basically contain anything. To extract a subset of boxes, use round parentheses. To extract the contents of the boxes, use braces. This analogy extends also to tables, where the notation is consistent.
Here's some docs on indexing cell arrays, where the difference is described in more detail:
https://uk.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html

Defining a function with multiple outputs that can't be organised into a matrix

Is there any natural way to define a MATLAB function with multiple outputs that cannot or are inappropriate to "stack" into a matrix? For example, what if I want a function f that returns a 3x3 matrix A and a 4x4 matrix B?
I'm really surprised that this would even be an issue in MATLAB. Because in Python, all we need to do is return A, B which returns a tuple of the two. However it seems that MATLAB doesn't quite support the idea of containers. As a non-elegant workaround, I can use a struct to put the two pieces of data in, and the function goes something like:
function re = f(x)
%f: returns two dimensional-inconsistent matrices A and B
% function body as follows
....
A = ...;
B = ...;
% put data into the struct 're'
re.A = A;
re.B = B;
end
Apart from possible performance issues, this approach looks very unnatural and clumsy. Is there any better approach?
In MATLAB you can return any number of outputs with this syntax:
function [A,B] = f(x)
A = ...;
B = ...;
end
that is an even elegant solution than tuples used in python.
You can even control the behavior with the number of inputs and outputs (nargin and nargout) and discard outputs with a tilde. More information here.
I cannot think of a more elegant syntax.
Usually when having several outputs, one should declare the function as follows:
function [out1, out2, ... , outN] = funcName(in1,...,inM)
...
end
MATLAB also allows you to alter the behavior of your function based on the amount of requested inputs/outputs via the nargin/nargout functions, respectively (you can think of this as a form of overloading).
For example, you can specify as one of the inputs an array indicating which outputs you want the function to give, then populate the varargout cell array accordingly:
function varargout = funcName(in1,...,whichOut)
...
for indO = 1:numel(whichOut)
switch whichOut{indO}
case 'out1'
varargout{indO} = out1;
case 'out2'
... etc
case 'out6'
varargout{indO} = out6;
end
end
then call it using [out6, out1] = funcName(inp, {'out6','out1'});
See also varargin.

Iterate names for LinearModel fit Objects in Matlab

I need to iterate through a large number of LinearModel fit Objects and have them stored in a logical way, preferably through an indexing method, e.g. model{x,y}. Less preferable is eval(), which I have tried and in any case is not working. I get the error Index exceeds matrix dimensions. - although the string expression works just fine outside of eval.
counter = 48;
str=strcat('model',+num2str(counter)); % Dynamic variable name
str1 = strcat(str,'=fitlm(tbl,modelspec)'); % Full string to be evaluated
eval(str1)
Ideally I wanted to do, while iterating through x
model{x,y} = fitlm(tbl,modelspec) % This is the equivalent expression
But the error I get is
"Assignment using {} is not allowed for a FitObject."
I think this is similar to this question - with no answer:
Dynamic Objects in Matlab
Managed to solve this by assigning to a cell structure.
e.g.
model = cell(3,3) % Pre-assign a cell structure
for x=1:3
for y=1:3
model{x,y} = fitlm(tbl,modelspec);
end
end

How can I preallocate a non-numeric vector in MATLAB?

I've often found myself doing something like this:
unprocessedData = fetchData(); % returns a vector of structs or objects
processedData = []; % will be full of structs or objects
for dataIdx = 1 : length(unprocessedData)
processedDatum = process(unprocessedData(dataIdx));
processedData = [processedData; processedDatum];
end
Which, whilst functional, isn't optimal - the processedData vector is growing inside the loop. Even mlint warns me that I should consider preallocating for speed.
Were data a vector of int8, I could do this:
% preallocate processed data array to prevent growth in loop
processedData = zeros(length(unprocessedData), 1, 'int8');
and modify the loop to fill vector slots rather than concatenate.
is there a way to preallocate a vector so that it can subsequently hold structs or objects?
Update: inspired by Azim's answer, I've simply reversed the loop order. Processing the last element first forces preallocation of the entire vector in the first hit, as the debugger confirms:
unprocessedData = fetchData();
% note that processedData isn't declared outside the loop - this breaks
% it if it'll later hold non-numeric data. Instead we exploit matlab's
% odd scope rules which mean that processedData will outlive the loop
% inside which it is first referenced:
for dataIdx = length(unprocessedData) : -1 : 1
processedData(dataIdx) = process(unprocessedData(dataIdx));
end
This requires that any objects returned by process() have a valid zero-args constructor since MATLAB initialises processedData on the first write to it with real objects.
mlint still complains about possible array growth, but I think that's because it can't recognise the reversed loop iteration...
In addition to Azim's answer, another way to do this is using repmat:
% Make a single structure element:
processedData = struct('field1',[],'field2',[]);
% Make an object:
processedData = object_constructor(...);
% Replicate data:
processedData = repmat(processedData,1,nElements);
where nElements is the number of elements you will have in the structure or object array.
BEWARE: If the object you are making is derived from the handle class, you won't be replicating the object itself, just handle references to it. Depending on your implementation, you might have to call the object constructor method nElements times.
Since you know the fields of the structure processedData and you know its length, one way would be the following:
unprocessedData = fetchData();
processedData = struct('field1', [], ...
'field2', []) % create the processed data struct
processedData(length(unprocessedData)) = processedData(1); % create an array with the required length
for dataIdx = 1:length(unprocessedData)
processedData(dataIdx) = process(unprocessedData(dataIdx));
end
This assumes that the process function returns a struct with the same fields as processedData.
You can pass in a cell array to struct of the appropriate size:
processedData = struct('field1', cell(nElements, 1), 'field2', []);
This will make a structure array that is the same size as the cell array.