guidataの使用について

2 次查看(过去 30 天)
Ryosuke Takahashi
Ryosuke Takahashi 2018-10-24
txtboxで記載した数値をguidataで取得し,別の関数で使用したいのですが上手くできませんでした。
guidataの使用が誤っているのだと思うのですが、どのように修正したら良いのかがわかりませんでした。 下記に途中まで作成したプログラムを記載します。 txtboxで記載した数値でプロットするsin波形のゲインを調節しようとしています。
初歩的な部分かもしれませんが,guidataの使用方法についてご教示のほどよろしくお願いいたします。
function buttonPlot
% Create a figure window
f = figure('Name','checking cutout start time');
% Create a UI axses1
ax1 = axes('Parent',f,...
'Units','pixels',...
'Position',[50, 220, 400, 170]);
% Create a UI axses2
ax2 = axes('Parent',f,...
'Units','pixels',...
'Position',[50, 20, 400, 170]);
% Create a txtbox1
txtbox1 = uicontrol(f,'Style','edit',...
'String','5',...
'Position',[460, 220, 100, 20],...
'Callback', @edit1_callback);
function edit1_callback(hObject,eventdata,handles)
input = str2double(get(hObject,'String'));
if isnan(input)
errordlg('You must enter a numeric value','Invalid Input','modal')
uicontrol(hObject)
return
else
display(input);
%guidata(object_handle,input)
end
end
% Create a push button
btn = uicontrol(f,'Style','pushbutton','String','update',...
'Position',[460, 120, 100, 20],...
'Callback', @pushbutton_callback);
%Create the wave1
x = linspace(0,2*pi,100);
y = sin(x)*rand;
plot(ax1, x, y,'k');
%Create the function for the ButtonPushedFcn callback
function pushbutton_callback(src,event)
x = linspace(0,2*pi,100);
y = sin(x)*rand%*input;
plot(ax2, x, y,'k');
end
end

采纳的回答

Etsuo Maeda
Etsuo Maeda 2018-10-29
“うまくいかなかった”ときは、“どのようなエラーがでたのか”の情報があったほうが回答が得やすくなるかと思います。
guidata関数のドキュメンテーションをまずはよくご確認ください。
https://jp.mathworks.com/help/matlab/ref/guidata.html
GUIDEを使っている場合、データを格納するときは、
function MyPutFcn (hObject, eventdata, handles)
handles.MyData = 0;
guidata(hObject, handles)
end
データを読み出すときは、
function MyGetFcn (hObject, eventdata, handles)
data = guidata(hObject);
end
のようにして読み出します。
GUIDEを使っていない場合は、guihandles関数を使うのが便利です。
function MyPutGetFcn()
hObject = figure(1);
handles = guihandles(hObject);
MyPutFcn(hObject, handles)
MyGetFcn (hObject, handles)
function MyPutFcn (hObject, handles)
handles.MyData = 0;
guidata(hObject, handles)
end
function MyGetFcn (hObject, handles)
data = guidata(hObject);
disp(data.MyData)
end
end
  1 个评论
Ryosuke Takahashi
Ryosuke Takahashi 2018-10-29
ご教示頂きありがとうございました。
ドキュメンテーションもう一度確認し、不明な場合にはお聞きするかもしれませんがよろしくお願い致します。

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by