为什么我收到这个错误使用cumsum时:“无效的数据类型。第一个输入参数必须是一个数字或逻辑。”?

12个视图(30天)
你好,
我有很少经验的一位心理学家在Matlab,我只是想运行代码(见附件)。函数是:
函数[H] = dfaedit (file_name、plot_flag outfile_flag, out_command_flag)
当调用输入参数的因数我总是收到错误:
错误使用cumsum
无效的数据类型。第一个输入参数必须是数字或逻辑。
错误dfaedit(44)行
和= cumsum (x);
(我已经尝试使用不同的文件类型。.dat, txt . csv),不同的输入变量(矩阵表、向量等)和从向量提取数据,但无论我得到同样的cumsum输入错误。整数的数据本身就是一个列值。
起初,我以为我的数据没有数值但尝试了上面提到的所有步骤后我的想法。有人能帮帮我吗?
如果这似乎是显而易见的:对不起)
谢谢!
4评论
李尔的大脑
李尔的大脑 2022年1月8日
我明白了。我给了他们所有的1。
所以我放入命令窗口:
dfaedit (' sample_test.txt ', 1, 1, 1)

登录置评。

接受的答案

Torsten
Torsten 2022年1月8日
试试这个:
函数主要
file_name =“sample_test.txt”;
H = dfaedit (file_name, 1, 1, 1)
结束
函数[H] = dfaedit (file_name、plot_flag outfile_flag, out_command_flag)
% DFA计算和情节去趋势波动
%
%的一维时间序列在一列文本文件名为“file_name”,
% DFA执行去趋势波动分析这些数据。它首先将
%的数据然后分区成不同的大小子集的大小
%不同MinBox(默认= 4)MaxBox(默认= 1/4长度的数据)。
%每个子集的大小,最好的拟合趋势线
%和均方根计算剩余(Q)。log10的
%的波动量(Q)策划反对log10点的一个子集。
%这回归的斜率估计分形的赫斯特指数(H)。
%维度可以计算为2 h。
%
%如果plot_flag = 1,那么创建一个回归的阴谋。如果outfile_flag = 1,文件名称,H
%和r ^ 2是附加到一个文本文件“dfa_results.txt”。如果out_command_flag = 1,数据
%,回归的基础图输出到命令窗口。
%
%的例子:dfa (“noise-f。dat ', 1, 1, 1)
%
%我使用了彭的C代码算法可在http://reylab.bidmc.harvard.edu。
%是彭等。讨论了量化的标度指数和交叉
% nonstationaryheartbeat时间系列的现象。混乱5:82 1995;87年。我向量化
%为MatLab初步计算。但是我没有触摸主循环。仍然不确定
%的算法用来计算均方根残留。
%
%的语法:
% dfa (file_name plot_flag、outfile_flag out_command_flag)
%
% r·c·施密特11-27-01
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
格式短g
% x = file_name;
fid = fopen (file_name);
data = textscan (fid检测器,' % f ');
fid =文件关闭(fid);
x = cell2mat(数据);
numberpoints =长度(x);
MinBox = 4;% minbox #的点在一个盒子里
MaxBox =升至* numberpoints;% maxbox 1/4长度的数据集
BoxSizeDensity = 4;
LogScaleFactor = (2.0, 1.0 / BoxSizeDensity);
指数= 1:numberpoints;
指数=重塑(指数、长度(指数),1);行,列向量变化百分比
%的初步计算
和= cumsum (x);
SumOfSumsSquared = cumsum(总和。*总和);
SumOfSums = cumsum(总和);
SSc = cumsum(指数。*总和);
%现在发现最适合线条和波动
%为每个盒子大小循环,从MinBox MaxBox
迭代= 1;
BoxSize = MinBox;
TempBoxSize = MinBox;
BoxSize < = MaxBox
s = 0.0;
r1 = 1。/ (BoxSize + 1.0);
侦破= 12.0 * r1 * r1 * r1 / (1.0 - r1 * r1);
vv = BoxSize * * BoxSize (2.0 + 1.0) / 6.0;
j = 2: numberpoints - BoxSize
dhh = SumOfSumsSquared (j + BoxSize) - SumOfSumsSquared (j - 1);
东华大学= SumOfSums (j + BoxSize) - SumOfSums (j - 1);
干热河谷= SSc (j + BoxSize) - SSc -东华大学* j (j - 1);
s = s + dhh(干热河谷*干热河谷+东华大学东华大学* * vv,干热河谷*东华大学* BoxSize) *侦破;
j = + 1;
结束
穴= (numberpoints - BoxSize) * (BoxSize + 1.0);
波动= sqrt (s /穴);
log_points_in_box(迭代,1)= log10 (BoxSize);
log_Q(迭代,1)= log10(波动);
show_results(迭代,:)=(迭代BoxSize波动log_points_in_box(迭代,1)log_Q(迭代,1)];
迭代=迭代+ 1;
%更新盒子的大小
TempBoxSize = TempBoxSize * LogScaleFactor;
轮(TempBoxSize) < BoxSize + 1.0
TempBoxSize = TempBoxSize * LogScaleFactor;
结束
BoxSize =圆(TempBoxSize);
结束
%都框;现在通过趋势线计算H
r_trend = corrcoef (log_points_in_box log_Q);
系数= polyfit (log_points_in_box log_Q 1);
log_points_in_box r_line = polyval(系数);
%计算维度和赫斯特
H =系数(1);
D = 2 h;
%在命令窗口中显示结果
如果out_command_flag = = 1
disp (“迭代点问Log10 (pnt) Log10 (Q) ')
disp (show_results)
结束
%显示结果图
如果plot_flag = = 1
h =轴(“位置”(0 0 1 1),“可见”,“关闭”);
轴(“位置”,(。1。2。75 .75])
情节(log_points_in_box log_Q,“b +”)
持有
情节(log_points_in_box r_line,“k”)
ylabel (“log10 (Q)”);
包含(“log10(分子集)”);
标题(file_name);
集(gcf,“CurrentAxes”、h);
str (1) = {[sprintf (' % 2.5 f '系数(1)),“x +”sprintf (' % 2.5 f '系数(2)),' = y, r ^ 2 = 'sprintf (' % 2.2 f 'r_trend (1、2) ^ 2),”,n =”sprintf (' % d '、长度(log_Q))]};
文本(。1,。02,str,“字形大小”10“颜色”,“r”);
str (1) = {(H = 'sprintf (' % 2.3 f 'H)' D = 'sprintf (' % 2.3 f 'D)};
文本(。5、.02 str,“字形大小”10“颜色”,“r”);
结束
% rd_results.txt结果附加到文件
如果outfile_flag = = 1
fid = fopen (“dfa_results.txt”,“一个”);
流(fid检测器,' % s % 4.4 f % 2.4 f \ n”,H file_name r_trend (1、2) ^ 2);
状态=文件关闭(fid);
结束
结束

答案(1)

明星黾
明星黾 2022年1月8日
(这似乎是编写的代码 2001年 )没有意义!
函数的第一个参数 “file_name” ,我 假设 将是一个字符数组。这是后来分配到 “x” 然后是 “和” 变量指定为 cumsum (x) 。做任何形式的计算在一个字符数组(没有首先转换成一个数字表示的字符数组的元素)是不可能的。
抛出错误。
请返回并查看文档来确定参数应该是。
选择= weboptions (“ContentType”,“文本”);
W = webread (“//www.tianjin-qmedu.com/matlabcentral/answers/uploaded_files/857175/dfaedit.m”选择)
W =
函数[H] = dfaedit (file_name、plot_flag outfile_flag, out_command_flag) % % % DFA计算和情节去趋势波动的一维时间序列在一列文本文件名为“file_name’, % DFA执行去趋势波动分析这些数据。第一集%数据然后分区成不同大小的子集大小%不等MinBox(默认= 4)MaxBox(默认= 1/4长度的数据)。%每个子集的大小,最好的拟合趋势线被发现%和均方根计算剩余(Q)。log10 %的波动量(Q)策划反对log10点的一个子集。%这回归估计的斜率的赫斯特指数(H)。分形维度可以计算为2 H %。% %如果plot_flag = 1,那么创建一个回归的阴谋。如果outfile_flag = 1,文件名称,H %和r ^ 2是附加到一个文本文件“dfa_results.txt”。如果out_command_flag = 1,回归情节的基础数据%输出到命令窗口。% %的例子:dfa (“noise-f。dat ', 1, 1, 1)% % I used the Peng's C code algorithm which is available at http://reylab.bidmc.harvard.edu. % It is discussed in Peng et al. Quantification of scaling exponents and crossover % phenomena in nonstationaryheartbeat time series. Chaos 1995;5:82-87. I vectorized the % preliminary calculations for MatLab. But the main loop I did not touch. Still not sure % of the algorithm they are using to calculate the root mean square residual. % % Syntax: % dfa(file_name,plot_flag, outfile_flag, out_command_flag) % % R. C. Schmidt 11-27-01 %------------------------------------------------------------------------------------- format short g x = file_name; numberpoints = length(x); MinBox = 4; % minbox #of points in a box MaxBox = .25*numberpoints; % maxbox set 1/4 the data length BoxSizeDensity = 4; LogScaleFactor = power(2.0, 1.0/BoxSizeDensity); index = 1:numberpoints; index = reshape(index, length(index), 1); %change row to column vector % Preliminary calculations Sum = cumsum(x); SumOfSumsSquared = cumsum(Sum.*Sum); SumOfSums = cumsum(Sum); SSc = cumsum(index.*Sum); % now find best fit lines and find fluctuation about the line % loop for each box size, from MinBox to MaxBox iteration = 1; BoxSize = MinBox; TempBoxSize = MinBox; while BoxSize <= MaxBox s = 0.0; r1 = 1./(BoxSize + 1.0); Det = 12.0*r1*r1*r1/(1.0 - r1*r1); vv = BoxSize*(2.0*BoxSize + 1.0)/6.0; for j = 2:numberpoints - BoxSize dhh = SumOfSumsSquared(j + BoxSize) - SumOfSumsSquared(j-1); dhu = SumOfSums(j + BoxSize) - SumOfSums(j-1); dhv = SSc(j + BoxSize) - SSc(j-1) - dhu*j; s = s + dhh - (dhv*dhv +dhu*dhu*vv - dhv*dhu*BoxSize)*Det; j = j + 1; end den = (numberpoints - BoxSize)*(BoxSize + 1.0); Fluctuation = sqrt(s/den); log_points_in_box(iteration,1) = log10(BoxSize); log_Q(iteration,1) = log10(Fluctuation); show_results(iteration,:) = [ iteration BoxSize Fluctuation log_points_in_box(iteration,1) log_Q(iteration,1) ]; iteration = iteration + 1; % update the box size TempBoxSize = TempBoxSize*LogScaleFactor; while round(TempBoxSize) < BoxSize + 1.0 TempBoxSize = TempBoxSize*LogScaleFactor; end BoxSize = round(TempBoxSize); end % got all boxes; now calculate H via trendline r_trend = corrcoef(log_points_in_box, log_Q); coefs = polyfit(log_points_in_box, log_Q,1); r_line = polyval(coefs,log_points_in_box); % calculate dimension and Hurst H = coefs(1); D = 2-H; %display results in command window if out_command_flag == 1 disp(' iteration points Q Log10(pnts) Log10(Q)') disp(show_results) end % display results in a figure if plot_flag == 1 figure h = axes('Position', [0 0 1 1], 'Visible', 'off'); axes('Position',[.1 .2 .75 .75]) plot(log_points_in_box, log_Q,'b+') hold on plot(log_points_in_box,r_line, 'k') ylabel('log10(Q)'); xlabel('log10(Points in Subset)'); title(file_name); set(gcf, 'CurrentAxes', h); str(1) = {[sprintf('%2.5f',coefs(1)),'x + ', sprintf('%2.5f',coefs(2)), ' = y, r^2 = ', sprintf('%2.2f',r_trend(1,2)^2), ', n = ', sprintf('%d',length(log_Q)) ]}; text(.1, .02, str, 'FontSize', 10, 'Color', 'r'); str(1) = {['H = ', sprintf('%2.3f',H) ' D = ', sprintf('%2.3f',D) ]}; text(.5, .02, str, 'FontSize', 10, 'Color', 'r'); end % append results to a file rd_results.txt if outfile_flag == 1 fid = fopen('dfa_results.txt','a'); fprintf(fid,'%s %4.4f %2.4f\n', file_name, H, r_trend(1,2)^2); status = fclose(fid); end '

类别

找到更多的在函数创建帮助中心文件交换

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!

翻译的