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

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

Stephen23
Stephen23 2017년 5월 19일
편집: Stephen23 2017년 5월 19일
"Why does class(dim) not work?" Because dim is not a variable in your workspace. It is the name of an optional input argument to the function sum.
"I do not understand why the error occurs"
Please show us the complete error message. This means all of the red text.
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!
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

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

Dear Guillaume,
thank you for your answer.
Next, I will show you the entire picture by sending you more details on the function:
Function msentropy.m (from Physionet.org)
>> addpath('E:\wfdb-app-toolbox-0-9-10\mcode')
x=load('01911m');
x = x(1,:);
minR=0.15*std(x);
[entropy,scale]=msentropy(x,[],[],[],[],[],2,2,3,minR,minR);
subplot(2,1,1);
plot(x);
subplot(2,1,2);
plot(scale,entropy)
--------------------------------------------------
function varargout=msentropy(varargin)
%
% [y,scale,info]=msentropy(x,dn,dm,dr,N,N0,minM,maxM,maxScale,minR,maxR)
%
% Wrapper to the Multiscale Entropy C code written by Madalena Costa (mcosta@fas.harvard.edu):
% http://physionet.org/physiotools/mse/mse-1.htm
%
% Calculates the multi scale entropy of a signal 'x'. A tutorial on Mulsticale
% entropy is available at:
% http://www.physionet.org/physiotools/mse/tutorial/
%
%
% Please cite these publications when referencing this material:
% Costa M., Goldberger A.L., Peng C.-K. Multiscale entropy analysis of biological signals. Phys Rev E 2005;71:021906.
% Costa M., Goldberger A.L., Peng C.-K. Multiscale entropy analysis of physiologic time series. Phys Rev Lett 2002; 89:062102.
%
% Also include the standard citation for PhysioNet:
% Goldberger AL, Amaral LAN, Glass L, Hausdorff JM, Ivanov PCh, Mark RG,
% Mietus JE, Moody GB, Peng C-K, Stanley HE. PhysioBank, PhysioToolkit, and PhysioNet: components of a new research resource for complex physiologic signals. Circulation 101(23):e215-e220 [Circulation Electronic Pages; http://circ.ahajournals.org/cgi/content/full/101/23/e215]; 2000 (June 13)
%
% Readers of may also wish to read:
% Costa M, Peng C-K, Goldberger AL, Hausdorff JM. Multiscale entropy analysis of human gait dynamics. Physica A 2003;330:53-60.
%
% Required Parameters:
%
% x
% Nx1 vector of doubles in which to caculate the multiscale entropy.
%
% Optional Parameters are:
% dn
% 1x1 double. Sets the scale increment to dn (1-40; default: 1).
% dm
% 1x1 double. Sets the m increment to dm (1-10; default: 1).
% dr
% 1x1 double. Sets the scale increment to dr (>0; default: 0.05).
% N
% 1x1 integer. Stop the analysis with row N.
% By default, analysis ends at row 39999, or at the end of the data set if there are fewer rows.
% N0
% 1x1 integer. Begin the analysis with row N0.
% By default, analysis begins with row 1.
% minM
% 1x1 integer betwee 1-10. Set the minimum pattern length for SampEn to minN (1-10; default: 2).
% maxM
% 1x1 integer betwee 1-10. Set the maximum m to maxM (1-10; default: 2).
% maxScale
% 1x1 integer betwee 1-40. Set the maximum scale for coarse-graining to maxScale (1-40; default: 20).
% minR
% 1x1 double >0. Set the minimum similarity criterion for SampEn to minR (>0; default: 0.15).
% maxR
% 1x1 double > 0. Set the maximum m to maxR (>0; default: 0.15).
%
%
% Outputs:
% y
% A 1xM vector of doubles corresponding to estimated sample entropies at each scale.
% scale
---------------------------------------------------
Script showing the errors:
>> addpath('E:\wfdb-app-toolbox-0-9-10\mcode')
x=load('01911m');
x = x(1,:);
minR=0.15*std(x);
[entropy,scale]=msentropy(x,[],[],[],[],[],2,2,3,minR,minR);
subplot(2,1,1);
plot(x);
subplot(2,1,2);
plot(scale,entropy)
Undefined function 'sum' for input arguments of type 'struct'.
Error in var (line 130)
xbar = sum(x, dim) ./ n;
Error in std (line 38)
y = sqrt(var(varargin{:}));
--------------------------------------------------
I apologize for the extensive description, but I would not be otherwise able to explain you what is going on.
I would be very thankful to you for any help and guidance.
Kind regards,
Massilon
Set a breakpoint on the line of code where you call std then run your script. When you reach that line, and MATLAB enters debug mode (the prompt should change to K>>) look at what x is. How would you (not MATLAB, but you) define the sum of that variable?
If 01911m is a MAT-file, the syntax you've used for load will make x into a struct array with one field per variable in the MAT-file. You probably don't want to take the std of the struct but you want to operate on one of the fields of the struct instead. If that's the case, create a new variable and store the appropriate field of the struct x into that new variable. For instance, if you want to operate on the field data of the struct x, define:
y = x.data;
minR = 0.15*std(y); % etc
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
Show us the output of
whos -file 01911m.mat

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by