writePosition does not work within a GUIDE
Show older comments
When I try to press the Go button of a GUIDE, I get this error
Undefined function 'writePosition' for input arguments of type 'double'.
My code is:
function go_Callback(hObject, eventdata, handles)
% hObject handle to ir_linea (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global s6
writePosition(s6, 0.5)
s6 was already instantiated correctly:
ardu = arduino ('COM51');
s6 = servo (ardu, 6);
How can I make it work within a GUIDE?
Answers (1)
Jan
on 22 Feb 2018
Using globals to provide data is a bad, bad, bad idea. You cannot control reliably, if the variable was modified anywhere.
Where do you create s6? In the OpeningFcn of the GUI? Then:
ardu = arduino('COM51');
handles.s6 = servo(ardu, 6);
guidata(hObject, handles);
And in the callback:
function go_Callback(hObject, eventdata, handles)
writePosition(handles.s6, 0.5)
But this is a bold guess only. I do not know, why the value of s6 is a double. Maybe this is expected? If you work with an arduino, you can check this easily by your own. Run the same code in the command window:
ardu = arduino('COM51');
s6 = servo(ardu, 6)
writePosition(s6, 0.5)
Does it work here? Is s6 a double here? The documentation says, it is an "object" - not really helpful.
Therefore I still guess, that your global variable s6 has been overwritten anywhere. And this would be typical of globals.
Categories
Find more on Loops and Conditional Statements 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!