How to make GUI window repeat a block of code until closed without blocking other operation

4 次查看(过去 30 天)
Hello. In essence, I'm trying to make a GUI that mimics the functionality of preview with a couple tweaks. Why can't I just use preview you ask?
1) The device I'm previewing (gigecam object) sends data as int16, but Matlab reads them as uint16 by default and I don't know how to change this. For calculation purposes this is easily fixed with a typecast, but as the majority of my frame is 0 +/- 40, the negative numbers get read as very large positive numbers in preview.
2) Preview chops the data down to uint8 (for speed?). I'd prefer to keep the full dynamic range of my frames.
3) I'd like to apply a colormap other than gray.
I've made a starter GUI to tinker with, and I am able to put the code I need into the OpeningFcn function to capture and display a frame as soon as the GUI is created. I tried to put that code inside an infinite while loop, but I think since it never got to exit OpeningFcn it never displayed anything and I had to close Matlab to break the loop. Clearly that was not the right way to do it.
I've tried Googling around, and haven't seen anything obvious. Is there a way I can safely have my GUI window execute a given block of code infinitely as long as it's open but still leave me the option to close the GUI (either with the 'x' button or programmatically)?
Thanks in advance!
[EDIT]: Typo in title fixed.

采纳的回答

Rik
Rik 2018-8-8
Yes, you can use e.g. a togglebutton. Use a drawnow somewhere in your loop (or a small pause) so its callback gets executed. Then you can use get in your loop to check for a buttonpress and use break to stop the loop.
  3 个评论
Rik
Rik 2018-8-9
You have to start at some place. I thought of GUI creation as so complicated I would never get the hang of it and at least reach the level where I wouldn't be terrible. However, it turns out me and GUIDE just weren't meant for each other.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(_);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
Rik
Rik 2018-8-9
The easiest way to be able to close your GUI, is to have a the output be a handle to your function. I would also suggest a start button, so the loop runs as a callback, which should leave you some options to still use other commands.
Instead of using a lazy global, you can use guidata, or getappdata to share a status between different functions.
As I understand it, Matlab will only update graphics objects when it encounters either a drawnow, a pause, or code has stopped executing. So I don't know if it is possible in Matlab to have your GUI continue updating while a uiwait is active.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by