How do I loop a program that waits for a .dat file to be updated?

I am trying to loop a program that calls data from a gui input.
In this case 'steps.dat' is created from a gui input and everytime the input changes so does the .dat file. Now I need to loop this program so that everytime the input changes the program runs, but i dont want it to run endlessly i want it to wait for the .dat file to be changed.
Thank you
while
F=load('steps.dat')
for motornum= 1:3
direc= sign(F(motornum))
steps=abs(F(motornum))
end
end

3 Comments

what kind of gui element you are using to get your .dat file?
The gui Is a forward/ inverse kinematic solution that calculates the number of steps for 3 diffirent motors to move. This command will than be sent to arduino.
Your gui element means when you import your .dat file to your gui by using a browse button, etc.
Define a callback function for that uicontrol object then you can do what ever you want, each time you clicked the button matlab calls the callback function.

Sign in to comment.

 Accepted Answer

Depending on the size of your .dat file this may work for your application:
while
while ~isfile('steps.dat'); end
F = load('steps.dat');
for motornum = 1:3
direc = sign(F(motornum));
steps = abs(F(motornum));
end
delete steps.dat
end

5 Comments

my .dat file is a 3x1 matrix. When I try this i get an error
"Invalid expression. Check for missing or extra characters."
Sorry I mis read / mis understood part of your question. This should now work:
while 1
while 1
if isfile('steps.dat') == 1
F = load('steps.dat');
break
end
end
for motornum = 1:3
direc = sign(F(motornum));
steps = abs(F(motornum));
end
delete steps.dat
end
Please let me know if it works!
The code never makes it to the for loop
Something from your GUI writing the file isent being initialized then. It worked for me when I dragged the file into the directory.
This is the line of code from my GUI that I am using to write the .dat file
save('steps.dat','FxStep','FyStep','FzStep','-ascii')

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Asked:

on 19 Nov 2018

Commented:

on 20 Nov 2018

Community Treasure Hunt

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

Start Hunting!