How can I change the elements of a matrix when hovering over its figure with the mouse?
Show older comments
I am trying to write a code that does the following: the user inserts the values for the row and column numbers for a matrix with values of 0. Then, when displaying the said matrix (as a white grided figure), whenever the mouse hovers over a matrix cell, the value of that becomes 1 and the cell's background is turned black. When hovering again on that cell, it turns back to 0 and white background, respectively. I'm a rather novice in MatLab so there might be simple stuff that I still didn't get right.
Here is my main .m file:
close all; clear all; clc; format long e;
r = input('How many rows do you want? ');
c = input('How many columns do you want? ');
M = zeros(r, c);
disp('Press a key when you want to start...');
clc;
stop = 'n';
figure
while stop == 'n';
imagesc(M); colormap('gray');
set(gcf,'WindowButtonMotionFcn', @mouseMove);
for i = 1: r,
for j = 1 : c,
if (x == i && y == j)
M{i, j} = 1 - M{i, j};
end
end
end
stop = input('Stop? (y/n) \n');
end
hold off;
And here's my mouseMove function:
function [x, y] = mouseMove
[x, y] = get (gca, 'CurrentPoint');
Please help me understand what I did wrong.
Accepted Answer
More Answers (0)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!