plot a graph from user input

27 visualizzazioni (ultimi 30 giorni)
Alina Abdikadyr
Alina Abdikadyr il 25 Feb 2023
Commentato: Star Strider il 26 Feb 2023
Hello everyone, could u please help me with a code. I need to plot the graph using the user's input values, but my code doesn't work
clear all; close all;
prompt1 = "Enter U_mean ";
U_mean = input(prompt1);
prompt2 = "Enter V_mean ";
V_mean = input(prompt2);
prompt3 = "Enter W_mean ";
W_mean= input(prompt3);
prompt4 = "Enter U_amplitude ";
U_amp= input(prompt4);
prompt5 = "Enter V_amplitude ";
V_amp= input(prompt5);
prompt6 = "Enter W_amplitude ";
W_amp= input(prompt6);
prompt7 = "Sampling frequency ";
freq= input(prompt7);
prompt8 = "Length of sampling ";
time= input(prompt8);
t=0:1/freq:time;
u=U_mean+U_amp*sin(100*t);
v=V_mean+V_amp*sin(100*t);
w=W_mean+W_amp*sin(100*t);
plot(u,t)
  3 Commenti
Steven Lord
Steven Lord il 25 Feb 2023
What does "doesn't work" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Adam Danz
Adam Danz il 25 Feb 2023
Using input() to get data from the user often leads to many problems. The input data are rarely validated and the user can enter anything, intentionally or unintentionally. The only reproducibility is by viewing the command history.
To address your question, we must know the inputs to reproduce the results and we need to understand what the expected results are so we can compare the actual/expected results.

Accedi per commentare.

Risposte (2)

Voss
Voss il 25 Feb 2023
Change this:
plot(u,t)
to this:
plot(t,u)

Star Strider
Star Strider il 25 Feb 2023
Use the inputdlg function instead. You can list everything you want responses to in one call to it, and even create default optons for the responses in the event that they are left blank. It has the additional advantage of not using the Command Window for inputs to it, avoiding Command Window problems.
The output from inputdlg is a cell array of strings, so your code will require ways to deal with that. Use str2double with the appropriate cell outputs (and similar functions including cellfun if necessary) to get the desired information from it.
I will help you to with it if necessary.
  2 Commenti
Alina Abdikadyr
Alina Abdikadyr il 26 Feb 2023
thank you! could you please show some examples
Star Strider
Star Strider il 26 Feb 2023
My pleasure!
They do not work in the online Run feature, so you will need to experiment with this offline —
dlgtitle = 'Input';
prompt1 = "Enter U_mean ";
prompt2 = "Enter V_mean ";
prompt3 = "Enter W_mean ";
prompt4 = "Enter U_amplitude ";
prompt5 = "Enter V_amplitude ";
prompt6 = "Enter W_amplitude ";
prompt7 = "Sampling frequency ";
prompt8 = "Length of sampling ";
prompts = {prompt1,prompt2,prompt3,prompt4,prompt5,prompt6,prompt7,prompt8};
defaultInput = {'0','0','0','0','0','0','0','0'};
dims = [1 25];
answerc = inputdlg(prompts,dlgtitle,dims,defaultInput);
answerv = num2cell(str2double(answerc));
[U_mean,V_mean,W_mean,U_amp,V_amp,W_amp,freq,time] = deal(answerv{:});
This works, however I cannot demonstrate it here. The ‘defaultInput’ cell array can be whatever you want. I filled it with zeros to test the code. The deal call distributes the numeric contents of the ‘answerc’ cell array to the various variables. (Check those to be certain I wrote them correctly and in the correct order.)
.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by