# Brushing when plotting a 2D Matrix with linkdata: brushes whole row of matrix instead of single data points.

5 views (last 30 days)
Sven Leach on 29 Dec 2021
Answered: Sven Leach on 1 Jan 2022
Normally, when brushing data in a plot, it is possible to brush single data points, no matter whether you plot a single vector or a 2D matrix. When turning on linkdata, however, brushing single data points is not possible anymore when plottong a 2D matrix. Instead, all the Y values corresponding to a certain X value are marked.
I would need to plot a 2D matrix at once (speed reasons) and be able to brush single data points and eventually set them to NaN and update the corresponding variable. Do I do something wrong? I'm in Matlab 2021b.
% Minimal example
figure;
Y = rand(20, 20);
plot(Y, 'k.', 'MarkerSize', 8);
% Turn on brush & linkdata
brush on

Sven Leach on 1 Jan 2022
I've spent some more hours on that problem and came up with that workaround that does what I need.
% Minimal example
figure;
Y = rand(20, 20);
p = plot(Y, 'k.', 'MarkerSize', 8);
% Turn on brush
brush on
% Only execute this after you brushed your data / set your brushed data to
% nan values:
% Gather brushed data
brushLOCS = cellfun(@find, get(p, 'BrushData'), 'Uni', 0);
% Set brushed data to NaN
for ichan = 1:numel(brushLOCS)
Y(ichan, brushLOCS{ichan}) = nan;
end

### More Answers (1)

Benjamin Kraus on 31 Dec 2021
The reason for the behavior you are seeing is because when you enable data linking (via linkdata) and then select data points, you are effectively selecting the rows of the input matrix. You can see that if you open the variable editor:
figure
Y = rand(20, 20);
plot(Y, 'k.', 'MarkerSize', 8);
brush on
openvar('Y')
After running that code, if you select points, you will see the corresponding rows highlighted in the variable editor.
I don't believe there is a way to disable that without either plotting the data as a vector or splitting the data up into one variable per column of the original data.
You mentioned "speed reasons" for why you are plotting all the data together in a single matrix, however the way you are plotting data in a matrix creates one line object for each column of the matrix, and it won't be substantially faster or slower than calling plot 20 individual times on 20 vectors. If you plot the data as a single vector, you will get better performance (because you will get a single line instead of 20 separate lines) and I suspect you will get the linking behavior you expect. The only downside is that to get linking to working you need to store your data in a variable with the vector.
This code will produce the same picture as above, but might be a tiny bit faster (depending on the size of the matrix) and will allow you to select individual points.
figure
Y = rand(20, 20);
[~,X] = meshgrid(1:20);
Yvec = Y(:);
plot(X(:),Yvec, 'k.', 'MarkerSize', 8);
brush on
If this code doesn't do what you are trying to do, can you explain in more detail how you are using data linking?
Sven Leach on 1 Jan 2022
Thanks for your response @Benjamin Kraus. I also saw a post that explained why it can be useful in certain situations to have the whole row linked. It's a pity the user cannot manually decide what is needed. My real matrices are much larger, e.g. 1000 x 129, plotting each row seperately takes forever and makes the plot very slow to interact with. I now build my own workaround that needed an additional function but does what I want it to do.