produce a code for GUI

조회 수: 5 (최근 30일)
Lim Xiao Hui
Lim Xiao Hui 2021년 6월 15일
답변: Aashray 2025년 8월 22일
Hello can i ask if i want to plot a x-y graph in GUI with below condition
constant x=[0,0.02,0.05],
let user insert input y=[num, num, num] and then plot (x,y)
how about the code should i write? Thank you for your reply.

답변 (1개)

Aashray
Aashray 2025년 8월 22일
If you just need a quick way for the user to enter three numbers for y and then plot against the fixed x = [0 0.02 0.05], you can use inputdlg to prompt for values and then plot them as in the below code:
X = [0 0.02 0.05];
answ = inputdlg({'Y(1):','Y(2):','Y(3):'}, 'Enter Y values', 1, {'0','0','0'});
if ~isempty(answ)
Y = str2double(answ);
figure; plot(X,Y,'o-','LineWidth',1.5); grid on;
xlabel('x'); ylabel('y'); title(sprintf('X=[0 0.02 0.05], Y=[%g %g %g]',Y));
end
When you run this, MATLAB will show a dialog with three boxes for the user to enter values of Y(1), Y(2), Y(3). After clicking OK, it will plot those points against [0 0.02 0.05].
You can refer to the below attached documentation links for reference:
  1. inputdlg”: https://www.mathworks.com/help/matlab/ref/inputdlg.html
  2. “str2double”: https://www.mathworks.com/help/matlab/ref/str2double.html

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by