Apart from main program, executing function continuously GUI matlab.

Dear experts,
this is Carlos. Now, I'm programming a GUI in Matlab.
In this GUI, I have some buttons and static texts. Particularly, I would like to change the text of a static box continuously, i.e., regardless the changes made in the main program.
Concretely, this static box takes the values from other hardware. The values obtained from this hardware have to be shown constantly. Simultaneously this static box shows the values from the hardware, I would like to press other buttons and carry out other functions.
The concept I look for, probably, is a kind of parallel programming. However, I think an easier solution should have.
Thank you in advance.
Carlos.

7 件のコメント

Carlos
Carlos 2017 年 2 月 23 日
No one?
Geoff Hayes
Geoff Hayes 2017 年 2 月 23 日
Carlos - you state that this static box takes the values from other hardware. How are you communicating, from within your GUI, to this other hardware? Do you have a timer that periodically communicates with this hardware? Please clarify and post some of your code.
Carlos
Carlos 2017 年 2 月 24 日
Hi Geoff, thank you for your reply. In order to communicate with the other hardware, I use the function "get" periodically. Since I need the information from this hardware each 0.5 seconds, my GUI performs a "get" and then pauses 0.5 seconds. No, I don't have a timer for this task. Could be a good solution to solve my problem? I tried to solve this issue using the callback function of the static text as follows:
function text5_Callback(hObject, eventdata, handles)
global distance
global flag;
while flag == 1
distance = save_pos;
pause(0.5)
set(handles.text5,'String',num2str(distancia))
end
The "get" command is within the function "save_pos.m". The flag depends on the state of the hardware. If this flag is 0, the static text should show other thing. However, the flag is 1, the code depicted above should be executed. I look forward to your responses. Thank you so much.
Walter Roberson
Walter Roberson 2017 年 2 月 24 日
At the moment, I cannot think of any hardware that uses get() as the call to retrieve data. I can think of a number of other calls such as fread or fgets or readdigital, but none of the interfaces that come to mind use get(). What kind of hardware interface are you using?
Carlos
Carlos 2017 年 2 月 24 日
Hi Walter, thank you for your comments. Fortunately, I take the values from the hardware perfectly using get(text). As I mentioned above, the main drawback is how can I show the values in the GUI constantly.
Geoff Hayes
Geoff Hayes 2017 年 2 月 24 日
Carlos - how is save_pos called? Is it from within your GUI or from the command line? Does it have a timer that periodically queries your device for the distance (or some other information)?
Walter Roberson
Walter Roberson 2017 年 2 月 24 日
Carlos, you did not answer the question about what kind of hardware you are interfacing? I have most of the toolboxes and I checked all of the get() functions but get() does not fetch from hardware for any of the ones I have.
The reason that it is important is that there may be other ways of fetching your data asynchronously.

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 2 月 23 日
編集済み: Jan 2017 年 3 月 8 日

0 投票

You can use a timer to update the GUI. Store the handle of the GUI object to be affected in the timer's UserData, such that it is available in the TimerFcn.
function myGUI
FigH = figure;
TextH = uicontrol('Style', 'Text', 'Position', [10, 10, 200, 24]);
TimerH = timer('UserData', TextH, 'TimerFcn', @myTimerFcn, 'Period', 1, ...
'ExecutionMode', 'fixedRate');
drawnow;
start(TimerH);
end
function myTimerFcn(TimerH, EventData)
TextH = get(TimerH, 'UserData');
set(TextH, 'String', datestr(now, 0));
drawnow; % Thanks Walter
end
Store the handle of the timer in the figure's UserData or ApplicationData (e.g. by handles.timerH = timer(...), such that it can be deleted in the CloseRequestFcn of the figure automatically.

6 件のコメント

Carlos
Carlos 2017 年 3 月 8 日
Hi Jan,
thank you for your reply.
I think this is the best way to solve this issue. However I still have a doubt. In my case, the text label has been created drawing directly in the GUI. So, I don't know how modify your program correctly. Concretely, I am trying the following:
function myGUI(handles)
TextH = handles.text5;
TimerH = timer('UserData', TextH, 'TimerFcn', @myTimerFcn, 'Period', 1, ...
'ExecutionMode', 'fixedRate');
drawnow;
start(TimerH);
function myTimerFcn(TimerH, eventData,handles)
TextH = get(timerH, 'UserData');
distancia = Guardar_pos();
pause(0.5)
set(TextH,'String',num2str(distancia))
As you can see, I'm trying to manage the text with "handles.text5". Can you help me? Thank you in advance. Carlos
Walter Roberson
Walter Roberson 2017 年 3 月 8 日
I would suggest adding the drawnow() to after the set() of TextH
Jan
Jan 2017 年 3 月 8 日
編集済み: Jan 2017 年 3 月 8 日
Thanks Walter, I've inserted it.
Carlos
Carlos 2017 年 3 月 9 日
Thank you so much guys. It works perfectly right now.
Evrim Yilmaz
Evrim Yilmaz 2017 年 6 月 8 日
Hi Jan, I have tried to compile this code in my matlab gui which is already working. But I get an error for "function mygui" line. Do you know why? Thanks
Walter Roberson
Walter Roberson 2017 年 6 月 9 日
You need to save that code to myGUI.m

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

質問済み:

2017 年 2 月 23 日

コメント済み:

2017 年 6 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by