How to output to GUI window?

I am getting Voltage values from Arduino that I would like to display on a GUI window. All I am looking for is a window that displays "Voltage = whatever it is at that instance" Can't seem to figure it out. Any help is appreciated.
I have the following;
a=arduino();
interv=10000;
time=1;
t=0;
%
while (time<interv)
time=time+1;
Voltage=readVoltage(a,'A0');
%
if Voltage > 1
disp(Voltage);
end
drawnow
end

1 Comment

The disp() should work, provided that the Voltage is greater than 1. You will not need drawnow. And you are missing an 'end'
It might make more sense to use a "for" than a "while"

Sign in to comment.

 Accepted Answer

OCDER
OCDER on 27 Jul 2018
Edited: OCDER on 27 Jul 2018
Is this something you were aiming for? Not sure how your GUI is made, but here's a sample .m file you could start from.
%myGUI.m
function myGUI
FH = figure('Visible', 'off');
set(FH, 'unit', 'pixel', 'position', [100 100 140 100]);
BTN = uicontrol(FH, 'Style', 'pushbutton', 'String', 'Read Voltage',...
'Position', [20 20 100 20],...
'Callback', @readVoltageInGUI);
TXT = uicontrol(FH, 'Style', 'text', 'String', 'Voltage = []', ...
'Position', [20 40 300 30], ...
'HorizontalAlignment', 'left');
set(FH, 'Visible', 'on')
function readVoltageInGUI(SOURCE, EVENT)
a = arduino();
interv = 10000;
time = 0;
while (time<interv)
time = time+1;
Voltage = readVoltage(a, 'A0');
if Voltage > 1
TXT.String = sprintf('Voltage (V) = %0.2f', Voltage);
drawnow
end
end
end
end

4 Comments

I think you get what I am trying to do. However, it's giving me the following error;
Error using myGUI/readVoltage Too many output arguments.
Error in myGUI/readVoltage (line 18) Voltage = readVoltage(a, 'A0');
Error while evaluating UIControl Callback.
Oops, I accidentally overriden the readVoltage matlab function with the GUI's readVoltage function for updating the GUI. Look at the edited answer above. the "readVoltage" GUI function is renamed to "readVoltageInGUI".
Thank you! Now I am getting the following error:
Error using myGUI/readVoltageInGUI (line 13) MATLAB connection to Arduino Uno at COM3 exists in your workspace. To create a new connection, clear the existing object.
Error while evaluating UIControl Callback.
I managed to make it work once by working on your script but then I messed with it too much.
Issue is caused because you called a = arduino multiple time.
Something like this will prevent it from trying to make another arduino object.
if ~exist('a', 'var')
a = arduino();
end
Here are more help for clearing arduino connections.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Tags

Asked:

on 27 Jul 2018

Commented:

on 30 Jul 2018

Community Treasure Hunt

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

Start Hunting!