Need help in MATLAB GUI

I am writting my question first time in MATLAB Central, I want to create the GUI using one push button and check box. In my GUI I read the text file in the while loop and plot the data of text file data using push button and check box. The purpose of push button plot the point plotting while the check box plots the data as trajectory When I push pushbutton so the simulation do just point plotting while hitting check box simulation performs continuous plotting from that current point. please help I spent 4 days but couldnt find the way two link the check box and push button in MATLAB GUI
I attach my code to this question

Answers (1)

Jan
Jan on 25 Jul 2017
Do not use global variables, because they make your life much harder. Globals are always a shot in your knee. Prefer storing the flag in the UserData or ApplicationData of the GUI. Search in this forum for "share data between callbacks".
line = fgetl(fid);
nums = str2num(line);
This will fail at the end of the file. Check for fgetl replying -1 before processing the data.
pause(0.0001) is meaningless, because the minimal pause is 0.01 seconds.
Use the standard code indentation to improve the readablility of the code. Ctrl-A Ctrl-I.
This does not do, what you want:
flag == 1
This compares the contents of flag with 1, but does not change its value. You mean
flag = 1;
But as said above: Do not use globals. It is easier to use the state of the checkbox itself:
while true
line = fgetl(fid);
if ~ischar(line)
break;
end
drawnow; % Give the GUI a chance to process events
checked = get(handles.checkbox1, 'Value');
if checked
...
else
...
end
end

6 Comments

Sairah
Sairah on 25 Jul 2017
Thank you jan simson for your reply But, how could I share that data between check box and pushbutton during the while loop? I feel trouble to share the data thats why I took global variable. As in text file I want, when I press push button so simulation starts and data starts plotting, when I hit the check box the starts plotting the trajectory when I remove the check from check box data plot like before check button was checked. I want to create this gui for tracking and positioning the objects
Jan
Jan on 25 Jul 2017
@Sairah Naveed: I still do not understand exactly, what the checkbox and the button should do. But you can "share" the status of the checkbox directly: Instead of useing a (bad) global variable, the 'Value' property of the checkbox can be used directly in the loop. There is no need for a further indirection.
If you really need to share a variable, please follow my advice and search for "share data callbacks" in this forum. You find a lot of instructions and it would not be efficient to post this again.
Note that the animation will be much faster, if you do not create new objects in each iteration, but update the properties of the existing objects. See e.g. https://www.mathworks.com/matlabcentral/answers/15971-animation-in-matlab. More examples are found, if you search for "animation" in this forum.
Sairah
Sairah on 26 Jul 2017
thank you Jan actually, I didn't get it where I put the while loop in my GUI program is it under GUI opening function or under pushbutton? or how I connect my while loop processing with checkbox as I want to control mu text file data plotting with the check box
Sairah
Sairah on 26 Jul 2017
Thank you I mage analyst for your reply as well
Jan
Jan on 27 Jul 2017
Edited: Jan on 27 Jul 2017
@Sairah: If the loop should start, when the button is pressed, insert it in the button's callback function. If the loop should start during the GUI is opened, insert it in the OpeningFcn, but then it blocks the creation of the GUI and I assume this is not wanted.
Did you see, that I've asked a question for clarification? I still do not understand what the button and the checkbox should do, so how could I suggest a solution? I've posted all my ideas about "cennecting" the loop and the contents of the checkbox already and I do not see, that you explain in any way, if my suggestions work for you or not. It looks like you ignore my contributions.

This question is closed.

Asked:

on 25 Jul 2017

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!