フィルターのクリア

How to read a numerical value from a static text box?

6 ビュー (過去 30 日間)
Vivien Bartis
Vivien Bartis 2021 年 1 月 4 日
コメント済み: Rik 2021 年 1 月 4 日
I tried it like this, but it won't use the value to calculate and plot the sine wave.
function pushbutton2_Callback(hObject, eventdata, handles)
f=num2str(get(handles.text6,'String'));
t=0:0.01:1;
b=sin(2*pi*f*t);
axes(handles.axes1);
plot(b);
This is where I calculate the f, with another pushbutton
function pushbutton1_Callback(hObject, eventdata, handles)
r=handles.edit1;
c=handles.edit2;
f=1/(2*pi*r*c);
set(handles.text6,'String',strcat(num2str(f*1000,2),' Hz'))
  1 件のコメント
Rik
Rik 2021 年 1 月 4 日
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.

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

回答 (2 件)

Maximilian Schönau
Maximilian Schönau 2021 年 1 月 4 日
Did you try str2double instead of num2str? ;)
  1 件のコメント
Vivien Bartis
Vivien Bartis 2021 年 1 月 4 日
okay, now I don't get the error, but I have still no plot.

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


Rik
Rik 2021 年 1 月 4 日
You need to strip the unit from the char array. Best practice is to keep numeric fields as pure as possible. With the uifigure-tools it is possible to enforce this during input, but in this case you need to remove it yourself:
function pushbutton2_Callback(hObject, eventdata, handles)
f=get(handles.text6,'String');%retrieve value from text field
f=strrep(f,'Hz','');%remove unit
f=num2str(f);%convert to value
t=0:0.01:1;
b=sin(2*pi*f*t);
plot(t,b,'Parent',handles.axes1);
% ^^ are you sure you don't want this instead of only b?
end

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by