在写入txt之前,Matlab绘制图形(逐段)和用户输入阈值

在写入txt之前,Matlab绘制图形(逐段)和用户输入阈值,matlab,Matlab,我一直在尝试使用此代码将x轴值峰值仅写入txt文件。此方法有效 %determine the bpm %count the dominant peaks in the signal fid = fopen('y1.txt','a'); %txt naming and append beat_count = 0; for k = 2 : length(pbcg)-1 if (pbcg(k) > pbcg(k-1) && pbcg(k) > pbcg(k+1) &

我一直在尝试使用此代码将x轴值峰值仅写入txt文件。此方法有效

%determine the bpm
%count the dominant peaks in the signal
fid = fopen('y1.txt','a'); %txt naming and append
beat_count = 0;
for k = 2 : length(pbcg)-1
    if (pbcg(k) > pbcg(k-1) && pbcg(k) > pbcg(k+1) && pbcg(k) > 1)
        beat_count = beat_count + 1;
    end
end

fprintf(fid, '%i\n', k); %open writer
fs = 100; %freq 100hz
N = length(pbcg);
duration_in_seconds = N/fs;
duration_in_minutes = duration_in_seconds/60;
BPM_avg = beat_count/duration_in_minutes;
fclose(fid); %close writer
但是问题来了,当我修改它来逐段绘制图形时,我设法做到了这一点,但当我的代码是这样时,问题是无法将x轴值写入txt文件。。我做错了什么

%plot segment by segment
data = pbcg;%data value from 1 to 10000
rows = reshape(data, 1000, numel(data)/1000)';%reshape the data into
%matrix by 1000 against total num of element in array and then / 1000)

fid = fopen('y1.txt','a'); %txt naming and append
beat_count = 0;

for e = 1:size(rows,1),
    %plot normal & with nodes together
    figure,plot(rows(e,:)),hold on,plot(rows(e,:),'ro');

    %if statement to find peak
    if (pbcg(k) > pbcg(k-1) && pbcg(k) > pbcg(k+1) && pbcg(k)> input('Key in the threshold value: '))
        beat_count = beat_count + 1;
        peaks(beat_count)=pbcg(k);
    end

    pause;%pause, on keypress go to next plot

    fprintf(fid,  'x_axis%i\n ', peaks); %open writer
end
fclose(fid); %close writer
我得到的结果是,即使在我输入阈值之后,也会得到整个峰值列表。

您需要将input语句置于循环之外,否则,每当所有其他条件都为真时,它将被计算多次。但实际上,有更有效的方法来做你想做的事情——基于这样一个事实,即matlab喜欢对语句进行矢量化,同时对整个数组进行操作。以下是一些想法:

rows = reshape( data, 1000, []); % the [] means "figure out what this dimension has to be so it works"

d1 = diff( rows, 1 ); % take "first derivative" along first non-singleton dimension
pkLocs = find(d1(1:end-1,:)>0 & d1(2:end,:) < 0 ); % peak = where derivative changes sign
threshold = input('Key in the threshold value:');
[pk_i pk_j] = ind2sub( size(d1), pkLocs );
pkVals = rows( sub2ind( size(rows), pk_i, pk_j) );
aboveThr = find( pkVals > threshold );
goodPk_i = pk_i( above_thr );
goodPk_j = pk_j( above_thr );

for j = 1:size(rows, 2)
  fprintf( 1, 'peaks found for segment %d:\n' );
  fprintf( 1, '%f  ', goodPk_i(goodPk_j == j ) );
end

免责声明未访问您的数据,也未访问Matlab-可能存在语法错误,但应该非常接近

我只想打印某个级别的阈值。如果我用'>1'替换输入'key in the threshold',它将只打印大于1的值。。比如说,如果我得到的部分片段运行不好,而整个片段都在阈值0.5左右,那么它们将被忽略。需要建议嗨@Floris很抱歉我的解释不好。我的意思是,比如说,我得到了10000个数据,所以我有10个1000个的绘图。我想将输入放入“for”循环的原因是,每次阈值都可能不同?。我不确定这是否符合标准?我是matlab新手,所以我发现你的代码对我来说有点难呵呵:X建议我请确定-至少把你的输入语句放在if语句之前,但放在outer for之后。我将用一个更简单的解决方案更新我的答案…我尝试了你的代码,但在“goodPk_I”上出现了一个错误,我不确定该怎么做。你收到的错误消息是什么?错误不是很具有描述性。。。看看我回答的第二部分你能做些什么?哦,对不起。。这是前面的错误消息。。输入阈值:1.6未定义的函数或变量“高于”。bpm第61行中的错误goodPk_i=pk_i高于thr;但是nvm我会看看你答案的第二部分。稍后将更新您:谢谢!
beat_count = 0; 
for e = 1:size(rows,1),
    %plot normal & with nodes together
    figure,plot(rows(e,:)),hold on,plot(rows(e,:),'ro');
    threshold = input('Key in the threshold value to use: ');

    % loop over this if statement to find peaks in this row
    for k = 2 : 999
      if (rows(e,k) > rows(e, k-1) && rows(e,k) > rows(e,k+1) && rows(e,k) > threshold)
        beat_count = beat_count + 1;
        peaks(beat_count)=rows(e,k);
        peak_x(beat_count) = k + 1000 * (e - 1);
      end
    end
    fprintf(1, 'press any key to continue!\n');
    pause; % pause, on keypress go to next plot
end

% since peaks array keeps growing, we should print it out all at once:
fprintf(fid, 'the following peaks were found:\n');
for ii = 1:beat_count
  fprintf(fid, 'x = %d; peak = %f\n ', peak_x(ii), peaks(ii)); %open writer
end
fclose(fid); % close the file once you're done