How come this codes works, but the other one doesn't.

1 view (last 30 days)
Collin Kerr
Collin Kerr on 5 May 2016
Edited: Stephen23 on 5 May 2016
(WORKS) %% variables
V = [17.43 11.46 8.11 4.23 2.29 30.18 19.84 14.05 7.34 3.96 34.85 22.91 16.22 8.47 4.57 13.18 19.84 14.05 7.34 3.96 17.43 11.46 8.11 4.23 2.29];
val = input('Choose a distance between 0 and 36 feet \n');
tmp = abs(V-val);
[xp xp] = min(tmp);
C = V(xp);
%% conditional
if xp <= 25 && xp >=21;
A = 75;
elseif xp <= 20 && xp >=16;
A = 60;
elseif xp <= 15 && xp >=11;
A = 45;
elseif xp <= 10 && xp >=6;
A = 30;
elseif xp <= 5 && xp >=1;
A = 15;
end
if xp == 1|| xp == 6 xp == 11 xp == 16 xp == 21;
M = 1;
elseif xp == 2 xp == 7 xp == 12|| xp == 17|| xp == 22;
M=2;
elseif xp == 3 || xp == 8 || xp == 13 || xp == 18 || xp == 23;
M=3;
elseif xp == 4 xp == 9 xp == 14 xp == 19 xp == 24;
M=4;
elseif xp == 5 || xp == 10 || xp == 15 || xp == 20 || xp == 25;
M =5;
end
if val== 0; M = 1; A = 0; end
%% display
fprintf('The setting that best fits your distance is %0.0f degrees and the %0.0fº string position, which wil give you %0.2f feet', A, M, C);
(DOES NOT WORK)(BELOW)
V = [13.36 12.37 11.36 12.40 10.30 11.43 7.70 9.19 4.60 6.66];
val = input('Choose a distance between 0 and 36 feet \n');
tmp = abs(V-val);
[xp xp] = min(tmp);
C = V(xp);
%% conditional
if xp <= 10 && xp >=9;
A = 75;
elseif xp <=8 && xp >=7;
A = 60;
elseif xp <= 6 && xp >=5;
A = 45;
elseif xp <= 4 && xp >=3;
A = 30;
elseif xp <= 2 && xp >=1;
A = 15;
end
if xp == 1|| xp == 6
M=1;
elseif xp == 2 xp == 7;
M=2;
elseif xp == 3 xp == 8;
M=3;
elseif xp == 4 xp == 9;
M=4;
elseif xp == 5 xp == 10;
M=5;
end
if val== 0; M = 1; A = 0; end
%% display
fprintf('The setting that best fits your distance is %0.0f degrees and the %0.0fº string position, which wil give you %0.2f feet', A, M, C);

Answers (1)

Image Analyst
Image Analyst on 5 May 2016
I've never seen %0.0f and don't know what you're thinking. Just use %f or something without zeros in it. If you want integers, use %d.
  1 Comment
Stephen23
Stephen23 on 5 May 2016
I don't know how '%0.0f' could be useful, but '%.0f' can be useful to keep the length of the output correct, e.g. when it should be equal to the number of digits:
>> sprintf('%d',1e19)
ans =
1.000000e+19
>> sprintf('%.0f',1e19)
ans =
10000000000000000000
This is sometimes a more reliable way to count digits than log10 or the like.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by