If I write commands in the command window: s = serial('COM3','BaudRate',9600); fopen(s); fprintf(s,'l0'); , it is good. How to write this text in pushbutton?
1 view (last 30 days)
Show older comments
Hi! I am doing a project for school and I need some help. If I write commands in the command window:
s = serial('COM3','BaudRate',9600);
fopen(s);
fprintf(s,'l0');
it works well. I create a button in GUI. How to write this text in pushbutton, so that it will be able to work correctly?
0 Comments
Answers (1)
Walter Roberson
on 8 May 2016
function pushdemo
fig = figure();
push = uicontrol('Style', 'push', 'String', 'Go!', 'Callback', @push_callback);
end
function push_callback(hObject, event)
s = serial('COM3','BaudRate',9600);
fopen(s);
fprintf(s,'l0');
disp('Sent it!')
pause(10);
fclose(s);
delete(s);
end
Since s is a variable local to the workspace of push_callback, it would be closed and deleted when the function returned; I just made it more clear that it was going to happen. If you do not want s to be deleted then you need to make sure that s lives on after the function returns.
2 Comments
Walter Roberson
on 8 May 2016
You tried to mix GUIDE with hand-written code.
If you are using GUIDE, have it create a push button, and in the callback for that pushbutton add the code
s = serial('COM3','BaudRate',9600);
fopen(s);
fprintf(s,'l0');
disp('Sent it!')
pause(10);
fclose(s);
delete(s);
but be sure to decide between COM3 (your code) and COM6 (your comment)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!