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

9 次查看(过去 30 天)
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 个评论
Massilon Toniolo da Silva
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 2017-5-19
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.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2017-5-19
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 个评论
Massilon Toniolo da Silva
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by