How can i display a uicontrol object at different locations?

4 ビュー (過去 30 日間)
Jonas Hemsen
Jonas Hemsen 2017 年 11 月 10 日
編集済み: Jonas Hemsen 2017 年 11 月 23 日
Hi, I have a GUI with two tabs. On both tabs I want to display the identic edit field so that changes to one of the fields are always synched to both edit fields. Is this possible or unintended by the way GUI programming works?
If so, is there a clean workaround or any other ideas how I could achieve the feature?
Thanks and tell me if you need more specification of the problem. Jonas
  2 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 11 月 10 日
Jonas - are you using GUIDE, App Designer, or are you programmatically creating the GUI?
Jonas Hemsen
Jonas Hemsen 2017 年 11 月 10 日
Hi Geoff and thanks for the reply. I use the programmatic way.

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

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 11 月 13 日
Hi Jonas - I suppose that you could assign the same callback to both edit controls. This callback would update a variable that could then be accessed by the other controls.
function sharedEditControl
hEdit1 = uicontrol('Style','Edit',...
'Callback', @EditControlCallback);
pos = get(hEdit1,'Position');
pos(2) = pos(2) + 100;
hEdit2 = uicontrol('Style','Edit',...
'Callback', @EditControlCallback, 'Position',pos);
sharedEditControlValue = [];
function EditControlCallback(hObject, eventdata)
sharedEditControlValue = get(hObject,'String');
fprintf('%s\n', sharedEditControlValue);
end
end
  1 件のコメント
Jonas Hemsen
Jonas Hemsen 2017 年 11 月 23 日
編集済み: Jonas Hemsen 2017 年 11 月 23 日
Hi Geoff, thanks for the help, you brought me on the right way. With little editing of your code I could solve the task. Here is what I changed and the code is shown below.
  • I created a figure first
  • I stored the handles of the edit fields in the figures UserData field to be able to access the edit fields later on
  • Additionally the handles must be present within the callback function in order to be able to change the value of them. This is why the main figure handle (with the edit fields handles stored in it) is passed into the callback.
  • And in the end obviously printing the edit field value in the command line with fprintf() is not what I intended so I just set the values of both edit fields to read out value.
hFig = figure;
hFig.UserData.handles.hEdit1 = uicontrol('Style','Edit',...
'Callback', {@EditControlCallback, hFig});
pos = get(hFig.UserData.handles.hEdit1,'Position');
pos(2) = pos(2) + 100;
hFig.UserData.handles.hEdit2 = uicontrol('Style','Edit',...
'Callback', {@EditControlCallback, hFig}, 'Position',pos);
function EditControlCallback(hObject, eventdata, handles)
handles.UserData.handles.hEdit1.String = eventdata.Source.String;
handles.UserData.handles.hEdit2.String = eventdata.Source.String;
end
Thanks again Jonas

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by