I Would like to create a variable array based upon user inputs.

조회 수: 1 (최근 30일)
CodeJunkie
CodeJunkie 2019년 1월 28일
댓글: CodeJunkie 2019년 1월 29일
I have a variable that the user inputs called Type allocated as such:
n=input('How many measurements are you determining uncertainty upon? ');
Type=size(n)';
Uo=1;
while Uo<=n;
if Uo==1;
Type(Uo)=input('What type of measurement is your first measurement? (Input 1,2,or 3 for Pressure,Temperature, and Flow Rate Respectively) ');
Uo=Uo+1;
elseif Uo>1;
Type(Uo)=input('What type of measurement is your next measurement? (Input 1,2,or 3 for Pressure,Temperature, and Flow Rate Respectively) ');
Uo=Uo+1;
end
end
I would like to use these values to create another variable matrix later in my code. I tried the following:
Factor=cell(n:1)
for iter = 1:n
if Type == 1
Factor{iter}=1
elseif Type == 2
Factor{iter}=1
elseif Type ==3
Factor{iter}= 4
elseif Type < 1
break
elseif Type > 3
break
end
end
Could someone shine some light into what I am doing wrong here? or is there a far better method I should be using to do this?
Thanks ahead of time for any help you can lend!
  댓글 수: 3
Adam Danz
Adam Danz 2019년 1월 28일
Also, could you provide some examples of inputs and expected outputs?
CodeJunkie
CodeJunkie 2019년 1월 29일
Sorry if I was a bit unclear basically the expected output I would like is something along the lines of if Type= [1,2,2,3,3,2,1] I would like to set a Variable factor to read type upon each iteration and return some factor. e.g. Factor=[5,6,6,8,8,6,5]. I was also hoping to make the factor possibly an equation so if I could keep it in matrix form that may be ideal rather than the cell form its currently in as one of the comments pointed out

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

채택된 답변

Jan
Jan 2019년 1월 29일
The code contains several strange commands.
Type=size(n)'
Are you sure? Without an explanation in the comment the readers can only guess, what you want. The question "How many ..." sound like you expect a scalar input, e.g. 5. Then size(5) replies [1,1] and transposing this is [1;1].
Later you fill the elements 1 to n of Type.
Factor=cell(n:1)
Do you mean:
Factor=cell(n, 1)
n:1 is the vector from n to 1, so most likely a scalar, if n>1. Then you create a {n x n} cell matrix.
When Type is a vector, them command if Type == 3 might not do, what you expect. if requires a scalar condition, therefore an all() is inserted implicitly. I guess you mean:
if Type(iter) == 3
  댓글 수: 1
CodeJunkie
CodeJunkie 2019년 1월 29일
yes i meant (n,1) sorry that was a typo in the code I wrote into here. you are also correct I should specify Type per iteration it seems my issue was that simple I appreciate your help vehemently!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by