produce a code for GUI

10 visualizzazioni (ultimi 30 giorni)
Lim Xiao Hui
Lim Xiao Hui il 15 Giu 2021
Risposto: Aashray il 22 Ago 2025 alle 5:52
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.

Risposte (1)

Aashray
Aashray il 22 Ago 2025 alle 5:52
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

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by