How to use structure properly?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-2-27
编辑:Stephen23
2017-2-27
@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);
采纳的回答
Stephen23
2017-2-27
编辑:Stephen23
2017-2-27
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:
- return a variable from the function, allocate it to a structure field: otpt.z = fun(...);.
- use one of the "merge structure" utilities on MATLAB FEX, e.g. catstruct.
- 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 个评论
Stephen23
2017-2-27
编辑:Stephen23
2017-2-27
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:
更多回答(1 个)
Steven Lord
2017-2-27
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!