How can I change the elements of a matrix when hovering over its figure with the mouse?
显示 更早的评论
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!