Clear Filters
Clear Filters

How to use structure properly?

1 view (last 30 days)
Xh Du
Xh Du on 27 Feb 2017
Answered: Steven Lord on 27 Feb 2017
Hi all,
I have a newbie question, how to use structural variable properly? A minimum example:
clear; clc;
no.inpt = 5;
inpt.a1 = rand(no.inpt);
inpt.a2 = rand(no.inpt);
inpt.a3 = rand(no.inpt);
[otpt] = teststruct(inpt);
[otpt] = teststruct1(otpt);
where the two functions are:
function [otpt] = teststruct(inpt)
otpt.x = inpt.a1 + 3 * inpt.a2 - inpt.a3;
otpt.y = 2 * inpt.a3;
and
function [otpt] = teststruct1(inpt)
otpt.z = inpt.x * 2 + inpt.y * 3;
After this, variable otpt only contains one subfield z which I understand that teststruct1 regenerate otpt. However, what I want is to let otpt keep three subfields x, y and z without renaming otpt after function teststruct. How could I achieve it?
Many thanks!
  2 Comments
Stephen23
Stephen23 on 27 Feb 2017
Edited: Stephen23 on 27 Feb 2017
@Xh Du: please do not edit your question so that answers do not make sense. This was the original code, where you clearly reallocate the same output variable and do not use it anwhere:
clear; clc;
no.inpt = 5;
inpt.a1 = rand(no.inpt);
inpt.a2 = rand(no.inpt);
inpt.a3 = rand(no.inpt);
[otpt] = teststruct(inpt);
[otpt] = teststruct1(inpt);
Xh Du
Xh Du on 27 Feb 2017
Hi Stephen,
Many thanks for your reply, apologize again for not explain the question as what I wanted, I'll be careful next time.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Feb 2017
Edited: Stephen23 on 27 Feb 2017
Have a look at these two lines:
[otpt] = teststruct(inpt);
[otpt] = teststruct1(inpt);
the first line creates otpt in that workspace, but then you never use this structure and on the second line the entire structure is thrown away because you define a new variable (which also happens to be a structure) using the same name. This has nothing to do with structures, you are just reallocating the same variable name.
Here are some ways to get the effect that you want:
  1. return a variable from the function, allocate it to a structure field: otpt.z = fun(...);.
  2. use one of the "merge structure" utilities on MATLAB FEX, e.g. catstruct.
  3. supply the structure as an input, add the field internally, and return it again:
function otpt = fun(otpt,inpt)
otpt.z = ...
end
personally I would use the last option: fast, neat, intuitive.
  2 Comments
Xh Du
Xh Du on 27 Feb 2017
Hi Stephen,
Apologize for the original question, I didn't explain clearly what I wanted. I've edited my question to make it clearer.
Your third suggestion seems correct but I tested, otpt still only has one subfield z, is there anything wrong?
Stephen23
Stephen23 on 27 Feb 2017
Edited: Stephen23 on 27 Feb 2017
If you want to keep the x and y fields then you need to make sure that they are in the output structure. The easiest way to do this is to define the output structure to be the input structure, then everything in the input gets returned. Like this:
>> no.inpt = 5;
>> inpt.a1 = rand(no.inpt);
>> inpt.a2 = rand(no.inpt);
>> inpt.a3 = rand(no.inpt);
>> otpt = teststruct0(inpt);
>> otpt = teststruct1(otpt);
>> otpt.x
ans =
0.50679 0.13162 1.18208 0.99448 1.06104
1.08214 1.62901 0.61569 0.43406 0.53366
-0.43888 1.93501 2.35304 3.66272 0.45414
2.20566 2.28234 1.95003 1.14059 1.81199
1.41425 1.88619 2.06689 1.80446 2.91711
>> opt.y
ans =
0.464202 1.226208 1.007491 0.194214 1.455524
0.088422 1.305071 0.437183 1.815059 0.896857
1.272788 0.560786 0.710052 0.363616 1.444390
1.646839 0.092584 1.821654 0.353976 1.265995
1.903759 1.194083 0.678222 0.671838 0.097544
>> opt.z
ans =
2.4062 3.9419 5.3866 2.5716 6.4887
2.4295 7.1732 2.5429 6.3133 3.7579
2.9406 5.5524 6.8362 8.4163 5.2414
9.3518 4.8424 9.3650 3.3431 7.4220
8.5398 7.3546 6.1684 5.6244 6.1269
using these functions:

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 27 Feb 2017
In this function:
function [otpt] = teststruct1(inpt)
otpt.z = inpt.x * 2 + inpt.y * 3;
otpt is created anew inside the teststruct1 function when you assign to otpt.z. Since it's a new variable, it only has the fields you explicitly assigned to it. I think you want to make otpt a copy of inpt first before modifying its z field.
function [otpt] = teststruct1(inpt)
otpt = inpt;
otpt.z = inpt.x * 2 + inpt.y * 3;

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!