Receive value in Matlab GUI

4 views (last 30 days)
Hello I'm working with Matlab GUI using GUIDE about show status from arduino(Ex:degree,current) and I have .m file program in Matlab to calculate and read variables when I press 'Start' button. I want program to receive realtime variables. Is there any way to do to make it work? I already try to get variables but It just give me only first time value. Here is my example of my .m file
function [B1, B2, B3, B4] = LogDataB(arduino)
mode=2;
fopen(arduino);
while mode
fprintf(arduino,'%s',1);
if(mode>=1)
B1=fscanf(arduino,'%d');
B2=fscanf(arduino,'%d');%Bx
B3=fscanf(arduino,'%d');%By
B4=fscanf(arduino,'%d');%Bz
end
mode=mode-1;
end
fclose(arduino);
Any idea please.
Thank you
Math

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Apr 2015
Thanadech - presumably the function LogDataB is called from the callback of your start button. Is this the case? Note that your while loop will only ever execute twice since your mode is initialized to two. As well, the results obtained for mode equal to two are overwritten for the case when mode is one. Is this intentional?
If you want this function to be called repeatedly once the start button is pressed, you may need to use a timer. See an example at http://www.mathworks.com/matlabcentral/answers/178682-collecting-a-variable-every-two-minutes which you should be able to adapt for your needs. In your case, the timer callback would call your above function.
  2 Comments
Thanadech Wunapeera
Thanadech Wunapeera on 26 Apr 2015
Thank you. Yes, Geoff. B1,B2,B3,B4 is called from the callback of start button to show value and loop execute twice is ok. I'm intent to do that because mode equal to one is nothing.
Would you please help me a little bit? I don't understand in TimerCallback function in your example. If I have a static textbox, How do I change in TimerCallback to show in textbox? It's ok to overwritten the old value in the box. I tried many times to change them but It's still error or nothing happen. I really new to Matlab.
Thank you so much
Math
Thanadech Wunapeera
Thanadech Wunapeera on 26 Apr 2015
Hahaha, Ok. I got it. Thank you so much for your help Geoff.

Sign in to comment.

More Answers (0)

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!