Making a button display a value in a message box

14 ビュー (過去 30 日間)
Edwin Froelich
Edwin Froelich 2018 年 6 月 14 日
回答済み: Jan 2018 年 6 月 16 日
I have a button that produces a value and puts it in a listbox. I would like to add a new button that displays that value in a message box. Please help!
  3 件のコメント
Edwin Froelich
Edwin Froelich 2018 年 6 月 15 日
Hello Jan,
The value produced is a mean. One button calculates that mean and I would like to store that value and retrieve it on call from a different button. I tried declaring a global variable but matlab didn't like when I did that. I am working in guide but I am trying to edit the code part of it to pass the value around.
Adam
Adam 2018 年 6 月 15 日
I use the guidata approach when using GUIDE and attach things to the handles struct.

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

回答 (1 件)

Jan
Jan 2018 年 6 月 16 日
Global variables are a bad programming practice, which can cause serious troubles and which impede the debugging massively. Therefore it is recommended to avoid globals - not only in Matlab, but in other programming languages also.
You can store data inside a GUI, e.g. in the UserData of the figure or any object, or in the ApplicationData. The latter works e.g. with guidata:
function OpeningFcn(hObject, EventData, handles)
% Define a default value:
handles.MeanValue = 'undefined';
% Store updated struct in the figure's ApplicationData:
guidata(hObject, handles);
end
function button1_callback(hObject, EventData, handles)
% Update the field MeanValue in the handles struct:
handles.MeanValue = mean(handles.Data);
% Store updated struct in the figure's ApplicationData:
guidata(hObject, handles);
end
function button2_callback(hObject, EventData, handles)
disp(handles.MeanValue);
end

カテゴリ

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