Undefined operator '-' for input arguments of type 'matlab.ui​.control.U​IControl'. - getting an Error and and cant find a solution.

9 Ansichten (letzte 30 Tage)
Hi everybody,
I have encountered a problem with a function that I'm trying to write. Basically, the goal is to plot a graph and then enable the user to select two different points (start and end). To enable corrections, I want to toggle that selection with buttons.
I had an automatic loop for this (without toggles and, as a result, no option to correct a faulty selection) using ginput() and it worked perfectly fine, however I seem to be ignorant of something regarding callback functions and how they function, because I cannot get that same code to work in one.
When I press enter to confirm the ginput, I get the following error message:
Undefined operator '-' for input arguments of type 'matlab.ui.control.UIControl'.
I have attached the relevant parts of the function script, hope someone can help.
f = figure(...)
plot(vector1,vector2)
c = uicontrol;
c.String = 'Start bestimmen';
c.Callback = @start_bestimmen;
function [x_start,y_start] = start_bestimmen(vector1,vector2)
[x,~]=ginput;
[~,idx_start] = min(abs(vector1-x));
x_start = vector1(idx_start);
y_start = vector2(idx_start);
hold on;plot(vector1(idx_start),vector2(idx_start),'rx');
end

Akzeptierte Antwort

Guillaume
Guillaume am 14 Jan. 2020
Matlab will always call your callback with two inputs. The first input is always the control handle triggering the callback, the 2nd input is some event specific argument, so yes when matlab call start_bestimmen, vector1 will be the uicontrol that triggers the callback and of course, you can't subtract from a control.
Since it doesn't appear that you need the control handle nor the event arguments, you could redefine the callback with an anonymous function:
c.Callback = @(~, ~) start_bestimmen(vector1, vector2); %ignore the standard callback arguments and call start_bestimmen with vector1 and vector2
%your callback code as it was:
function [x_start,y_start] = start_bestimmen(vector1,vector2)
%...
However, note that if vector1 or vector2 changes after you've defined the callback, these changes won't be seen by start_bestimmen. I assume that's not an issue for your use case.
  2 Kommentare
Benjamin Sprick
Benjamin Sprick am 14 Jan. 2020
Thanks a lot! Didnt know that.....your solution works perfectly so far, and you guessed right - I dont need to refresh the vectors after the callback.
Marco Boesso
Marco Boesso am 27 Nov. 2020
Hey dude, thanks a lot for your comments because I'm having issues with this kind of stuff.
Your explaination really does the job, but I need the change in my variables to be seen.
Let's get clear: I define a variable called I, set as 0. Whenever I push the left button, I want it to decrease (so, -1, -2, -3, -4, etc...). Whenever I push the right button, I want it to increase (1, 2, 3, 4,...).
But, as you said, the function does not see the new variable, unfortunately. It doesn't return the changes.
Pasting my current code:
close all
clear all
clc
I = 0;
fgh = figure('position',[850,600,210,60]);
%
uicontrol('Parent',fgh, 'Style', 'Pushbutton', 'String', 'Go left',...
'Position',[0.1,0.1,50,20], 'Callback', @(~, ~) Ileft(I));
uicontrol('Parent',fgh, 'Style', 'Pushbutton', 'String', 'Go right',...
'Position',[100,0.1,50,20], 'Callback', @(~, ~) Iright(I));
%
function I = Ileft(I)
I=I-1
end
%
function I = Iright(I)
I=I+1
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by