Pourquoi est ce que je reçois le message d'erreur 'Error while evaluating UIControl Callback.' ?

2 Ansichten (letzte 30 Tage)
Bonjour,
J'écris un programme dans lequel je souhaite superposer une grille et une image. Je veux pouvoir déplacer ma grille selon les 4 directions du plan, mais j'ai un message d'erreur que je n'arive pas à contourner.
Si quelqu'un aurait la possibilité de modifier mon code afin qu'il soit opérationnel, je lui en suis reconaissant !
% Création de la grille de points au hasard
x = linspace(0, 100, 200);
y = linspace(0, 100, 200);
[X, Y] = meshgrid(x, y);
% Affichage de l'image et de la grille
figure;
imshow('votre_image.jpg'); % Remplacez 'votre_image.jpg' par le chemin de votre image
hold on;
plot(X, Y, 'r.', 'MarkerSize', 10);
% Création des boutons de commande pour déplacer la grille
leftBtn = uicontrol('Style', 'pushbutton', 'String', 'Gauche', ...
'Position', [10 10 60 30], 'Callback', @(~,~) moveGrid(-1, 0));
rightBtn = uicontrol('Style', 'pushbutton', 'String', 'Droite', ...
'Position', [80 10 60 30], 'Callback', @(~,~) moveGrid(1, 0));
upBtn = uicontrol('Style', 'pushbutton', 'String', 'Haut', ...
'Position', [150 10 60 30], 'Callback', @(~,~) moveGrid(0, 1));
downBtn = uicontrol('Style', 'pushbutton', 'String', 'Bas', ...
'Position', [220 10 60 30], 'Callback', @(~,~) moveGrid(0, -1));
% Fonction de rappel pour déplacer la grille
function moveGrid(dx, dy)
% Obtenir les coordonnées actuelles de la grille
h = findobj(gca, 'Type', 'line');
xdata = get(h, 'XData');
ydata = get(h, 'YData');
% Déplacement de la grille
newX = xdata + dx;
newY = ydata + dy;
% Mise à jour de l'affichage de la grille
set(h, 'XData', newX, 'YData', newY);
end

Akzeptierte Antwort

Avni Agrawal
Avni Agrawal am 3 Apr. 2024
Hi @Mathias Noll, I understand that you are trying to overlay grid. The error "Operator '+' is not supported for operands of type 'cell'" occurs because MATLAB cannot use the + operator on cell arrays, which is how XData and YData are returned when multiple plot objects are involved.
To fix this, ensure operations are on numeric arrays. A solution is to avoid using global variables and directly manipulate numeric arrays for the grid's position, updating the entire grid as a single object. This approach simplifies handling and avoids issues with cell arrays by replotting the grid points with updated positions.
function createInteractiveGrid()
% Initial grid setup
x = linspace(0, 100, 200);
y = linspace(0, 100, 200);
[X, Y] = meshgrid(x, y);
deltaX = 0;
deltaY = 0;
% Display image and grid
fig = figure;
imshow('your_image.jpg'); % Replace with your image path
hold on;
hGrid = plot(X(:), Y(:), 'r.', 'MarkerSize', 10);
% UI controls to move the grid
uicontrol('Style', 'pushbutton', 'String', 'Left',...
'Position', [10 10 60 30], 'Callback', @(~,~) moveGrid(-1, 0));
uicontrol('Style', 'pushbutton', 'String', 'Right',...
'Position', [80 10 60 30], 'Callback', @(~,~) moveGrid(1, 0));
uicontrol('Style', 'pushbutton', 'String', 'Up',...
'Position', [150 10 60 30], 'Callback', @(~,~) moveGrid(0, 1));
uicontrol('Style', 'pushbutton', 'String', 'Down',...
'Position', [220 10 60 30], 'Callback', @(~,~) moveGrid(0, -1));
% Callback for moving the grid
function moveGrid(dx, dy)
deltaX = deltaX + dx;
deltaY = deltaY + dy;
newX = X + deltaX;
newY = Y + deltaY;
set(hGrid, 'XData', newX(:), 'YData', newY(:));
end
end
This solution encapsulates the entire functionality within a function, avoiding global variables. It maintains the offset (`deltaX`, `deltaY`) to shift the grid. The grid is moved by updating these offsets and then reapplying them to the original grid coordinates (`X`, `Y`), followed by updating the plot data directly. This approach should be more robust and avoid the issues with cell arrays you encountered.
Please refer to below documentation for better understanding:
I hope this helps.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!