MATLAB Answers

Wait for user input to select region of data from plot and save it to a variable

5 views (last 30 days)
Rakshith Badarinath
Rakshith Badarinath on 10 Apr 2021
Answered: Monisha Nalluru on 16 Apr 2021
I have a list of files named sequentially (E.g. "Run1.csv", "Run2.csv", ..... ) that I am reading using "readtable" and plotting the contents using a for loop. I am generating a figure for each file in the for loop.
%% Tidy up workspace
clc; clear; close all;
%% Load raw data
ir_dir = "Path to directory 1\";
ptv_dir = "Path to directory 2\";
% Constants
num_runs = 28; % Number of files
for i=1:num_runs
% create dynamic file path
ir_fname = "Run" + num2str(i) +".dat";
ptv_fname = "Run" + num2str(i) +".csv";
ir_fullpath = ir_dir + ir_fname;
ptv_fullpath = ptv_dir + ptv_fname;
% Read data
tblIR = readtable(ir_fullpath);
tblPTV = readtable(ptv_fullpath);
% plot data
hold on;
hold off;
grid on;
xlabel('Time (hh:mm:ss.sss)');
ylabel('Temperature (C)');
title("Run #" + num2str(i));
% Wait for user input to select data and save it to a variable (data
% brushing?? -- How to automate this?)
% After user has selected data using data brushing and data is saved,
% continue with for loop execution
clear tblIR;
clear tblPTV;
Here is an example of output plot that I get after each iteration of for loop:
After the plot is generated, I want the for loop to pause for user to select a region of data in the plot. Something like this:
Upon selection of the data using data brush, I want to data to be saved into a variable in the workspace and continue on with next iteration of for loop.
Is it possible to automate this data brushing for each iteration of for loop? Is there a way to invoke data brushing automatically when a figure is generated? and to automatically save the results of data brushing to a variable?

Answers (1)

Monisha Nalluru
Monisha Nalluru on 16 Apr 2021
BrushData is property present on plot handle used to set and get brushed data on a plot
In order to get the brushed data and save to workspace programmatically, here is the following example
hold on;
ax = gca;
for i=1:length(ax.Children) % loops through all the plot in figure
indices = find(ax.Children(i).BrushData); % gives the indices which are brushed
% save the data into required variable
Inorder to set brushed data programmatically then you can use the following structure,
ax.Children(i).BrushData = [0 1 1 1 1 1 1]; % pass the indices which wants to brush as 1 and remaining as 0

Community Treasure Hunt

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

Start Hunting!