MATLAB: How to assign a structure with fields (incl the values in the fields) as an output in function

fieldsoutputstructure

Hello,
My question is the following: How to assign a structure with fields (incl the values in the fields) as an output in function?
Let's say the code is something like this
function output =example(x)
struct(1).field1=x+1
struct(2).field1=x+2
struct(1).field2=x/5
struct(2).field2=x/6
output=struct(1:2)
_________________
How do I assign the fields with their values to the output variable. In this example here I have two fields, but in the actual function I have a lot more and all of these fields have values which I would like to use later in the workspace.
Any ideas?

Best Answer

function Sout = myfunction(x)
S(1).field(1) = x ;
S(2).field(2) = 2*x ;
% etc.
Sout = S ; % just copy
% end of my function
Do not use names like field1, field2, but use an array (of doubles, cells or even structs)!!!