Matlab绘图图(按段划分)和用户输入阈值,然后写入txt [英] Matlab plot graph(segment by segment) and user input threshold value before writing to txt

查看:91
本文介绍了Matlab绘图图(按段划分)和用户输入阈值,然后写入txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使用此代码(此方法有效)将x轴值(仅峰值)写入txt文件.

I have been trying to write the x-axis values (peak only) to txt file using this code (this method is working).

%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

但是当我修改它以逐段绘制图形时,问题就来了(我设法完成了此工作),但是当我的代码是像这样..我做错了什么吗?

But the problem comes in here when i have modified it to plot the graph segment by segment (i manage to get this done) but the problem is not able to write the x-axis value to the txt file when my code is like this.. anything i done wrong?

%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

即使输入阈值,我得到的结果也是整个峰列表.

the result i got was the entire list of peaks even after i enter the threshold value.

推荐答案

您需要将input语句放在循环外,否则它将被评估多次(每次其他所有条件都成立时).但是实际上,基于matlab喜欢对语句进行矢量化的事实(一次在整个数组上进行操作),有很多更有效的方法可以执行所需的操作.这里有一些想法:

You need to put the input statement outside the loop, or it will be evaluated multiple times (every time all the other conditions are true). But really, there are far more efficient ways to do what you want - based on the fact that matlab likes statements to be vectorized (operate over the entire array at once). Here are a few ideas:

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

看看是否可以帮助您弄清楚!

See if that helps you figure it out!

一个简单的答案:您需要在k上创建一个内部循环,并对代码进行其他一些更改,如下所示:

A simpler answer to the problem you stated: you need to create an inner loop over k, and make a few other changes to your code, as follows:

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

免责声明,其编写时无权访问您的数据,也无权访问Matlab-可能存在语法错误,但应非常接近

disclaimer written without access to your data, and without access to Matlab - possible there is a syntax error but should be very close

这篇关于Matlab绘图图(按段划分)和用户输入阈值,然后写入txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆