plot a graph from user input

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 个评论

What seems to be the issue? Are you getting an error? or are you not getting the correct output?
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.
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.

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2023-2-25

0 个投票

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 个评论

thank you! could you please show some examples
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.)
.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by