1
votes

I have new questions after meeting the criteria from these Matlab plot graph(segment by segment) and user input threshold value before writing to txt and Matlab load entire file but show and plot segment by segment

I was tasked to plot the graph and show the peak nodes (those that is > threshold) but only manage to plot the nodes and it is stuck here:

what I can

I'm trying to plot it out what this pic shows but I'm having difficulty:

what I want

Here's my code:

for e = 1:size(rows,1),
    %plot normal graph first, hold it before plotting nodes
    %so that it is combined
    figure,plot(rows(e,:)), hold on;

    %put the 'INPUT' statement outside the loop, or it will be evaluated 
    %multiple times (every time all the other conditions are true)
    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);
            plot(rows(e,peak_x(beat_count)),'ro');
        end
    end
    %pop up text to plot new segment
    fprintf(1, 'press any key to continue!\n');

    % pause, on keypress go to next plot
    pause; 
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

I have 3 questions actually but I want to solve it 1 by 1. So the first 1 will be

  1. Plot the graph and show the peak nodes 'O' to those that is > than the threshold input by user (I manage to plot the peak 'O' but it's all stuck at 1 place)

  2. As the graph is being segment into every 1000, is it possible to increase the value on the x-axis? Like the first plot is from 0 to 1000 and the second will be 1001 to 2000 and so forth untill the entire length of the data.

  3. Allow user input from whichever segment they want to start, for example I can key in a value so I will plot from 3001 to 4000 and each time after I key in a threshold value it will write the output to text file instead of writing all the output to the textfile all at the end. (If anything error occur halfway, you need to redo everything so this is to prevent from repeating the entire process if something happen halfway and I can start from where I stop, too)

1

1 Answers

0
votes

The plot function takes two arguments, the x values and the y values. If you want to plot from 0 to 1000, set the x value:

x = ((e-1)*1000):(e*1000);
plot(x, rows(e,:));
...

In order to plot the 'o' at the correct x value, include the x argument:

plot(peak_x(beat_count), rows(e,peak_x(beat_count)),'ro');
...

If you want to print data to a file every time the user finishes a segment, open the file for appending right after the above line:

fid = fopen('OUTPUTFILE.txt', 'at');
fprintf(fid, 'x = %d; peak = %f\n', peak_x(beat_count), peaks(beat_count));
fclose(fid);