How can I make this code better?
6 views (last 30 days)
Show older comments
Furkan Yürümez
on 29 Mar 2020
Answered: Image Analyst
on 29 Mar 2020
Hi everyone! I just wrote the code below, but as you can see it does not seem good. I mean ı want to make it better. Can anyone help me make this better?
C1=menu('First digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White');
switch C1
case 1
C1=0;
case 2
C1=1;
case 3
C1=2;
case 4
C1=3;
case 5
C1=4;
case 6
C1=5;
case 7
C1=6;
case 8
C1=7;
case 9
C1=8;
case 10
C1=9;
end
C2=menu('Second digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White');
switch C2
case 1
C2=0;
case 2
C2=1;
case 3
C2=2;
case 4
C2=3;
case 5
C2=4;
case 6
C2=5;
case 7
C2=6;
case 8
C2=7;
case 9
C2=8;
case 10
C2=9;
end
C3=menu('Third digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White');
switch C3
case 1
C3=0;
case 2
C3=1;
case 3
C3=2;
case 4
C3=3;
case 5
C3=4;
case 6
C3=5;
case 7
C3=6;
case 8
C3=7;
case 9
C3=8;
case 10
C3=9;
end
C4=menu('Tolerance digit','Missing','Silver','Gold');
switch C4
case 1
C4=0.2;
case 2
C4=0.1;
case 3
C4=0.05;
end
A=((C1*10)+C2)*10^C3;
R1=A+(C4*A);
R2=A-(C4*A);
fprintf('Nominal Value is %d k?, Range is %d to %d' , A,R1,R2)
Second question ;
is there any way to use "menu" command with several values ? or any alternatives to that command ı mean can ı use one menu command to get C1,C2 and C3?
thank you in advance :)
0 Comments
Accepted Answer
Ameer Hamza
on 29 Mar 2020
Why would you even use switch-case for such trivial thing. It is just subtracting 1 from the input number. Try this
C4_array = [0.2 0.1 0.05];
C1=menu('First digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White')-1;
C2=menu('Second digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White')-1;
C3=menu('Third digit','Black','Brown','Red','Orange','Yellow','Green','Blue','Violet','Gray','White')-1;
C4_idx=menu('Tolerance digit','Missing','Silver','Gold');
C4 = C4_array(C4_idx);
A=((C1*10)+C2)*10^C3;
R1=A+(C4*A);
R2=A-(C4*A);
fprintf('Nominal Value is %d k?, Range is %d to %d\n' , A,R1,R2)
For any other simpler option, please specify the version of MATLAB you are using.
2 Comments
More Answers (1)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!