Need help in MATLAB GUI
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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
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
on 25 Jul 2017
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
on 26 Jul 2017
Sairah
on 26 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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!