Undefined function 'sum' for input arguments of type 'struct' and Error in std.

9 views (last 30 days)
Problem 1:
Undefined function 'sum' for input arguments of type 'struct':
Error in var: xbar = sum(x, dim) ./ n;
Note: I have enter into command window:
1) class(x)and got:
>> class(x)
ans =
struct
2) class(dim)and got:
>> class(dim) Undefined function or variable 'dim'. Why does class(dim) not work? -------------------------------------------------------------------------------------- Problem 2:
Error in std:
y = sqrt(var(varargin{:}));
I do not understand why the error occurs. What would be most likely reasons and how to sort it out? ------------------------------------------------------------------------------------- Many thanks for any help and guidance!
  3 Comments
Massilon Toniolo da Silva
Massilon Toniolo da Silva on 19 May 2017
Dear Stephen, I thank you very much for this valuable information! My Matlab skills are quite limited and I often miss the point. Still, I am getting an error because my input signal x is of the class structure. What should I do to get my script working. I do not understand struct properly. Thanks very much!
Adam
Adam on 19 May 2017
doc struct
will tell you about structs, which are just containers for a heap of variables (fields as they are called, for structs). You must know what data you actually want to use though. Clearly you don't want to use the struct in your maths so just access whichever of its fields is the one you want.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 19 May 2017
As you've clearly shown, x is a structure. As the error message also clearly tells you, the sum function is not defined for strutures. Matlab has no idea what the definition of the sum of a structure should be. Neither have we, since it does not make sense generically. What should the sum of
x = struct('ID', {1, 2}, 'name', {'joe', 'jack'})
should be?
With the limited information given, we can't tell you how to solve the issue. Maybe you meant to sum something else, in which case pass the correct variable to sum. Maybe you meant to sum a specific field of the structure(s), in which case you need to explicitly pass these fields to sum. Maybe, you meant to do something else.
  4 Comments
Massilon Toniolo da Silva
Massilon Toniolo da Silva on 20 May 2017
Hi Steven, thank you for your answer. I have used y = x.data; and did not work. What should data stand for? Could you perhaps clarify on that? For instance, with an example? Many thanks Massilon

Sign in to comment.

More Answers (0)

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!