필터 지우기
필터 지우기

plot a graph from user input

조회 수: 13 (최근 30일)
Alina Abdikadyr
Alina Abdikadyr 2023년 2월 25일
댓글: Star Strider 2023년 2월 26일
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
Steven Lord
Steven Lord 2023년 2월 25일
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 2023년 2월 25일
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개)

Voss
Voss 2023년 2월 25일
Change this:
plot(u,t)
to this:
plot(t,u)

Star Strider
Star Strider 2023년 2월 25일
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
Alina Abdikadyr
Alina Abdikadyr 2023년 2월 26일
thank you! could you please show some examples
Star Strider
Star Strider 2023년 2월 26일
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.)
.

댓글을 달려면 로그인하십시오.

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by