Question about sprintf function.

Here's my syntax:
bm= input ('value');
val = 43 ;
A = sprintf('%d',val);
%if i declare A(1), ans is 4, which is correct.
c=0;
A1 = A(1) .* (bm^(c))
Why is it that if my bm = 8, A1 = 52? its like 4 * 8 = 52. i think my error here is the sprintf function.

 채택된 답변

Image Analyst
Image Analyst 2012년 9월 29일

0 개 추천

Why not just do
A1 = val;
???? bm^c is just 1, and I'm not sure if you really wanted to take the first digit of 43. If you did, you could just do
A1 = floor(A/10); % As long as A is less than 100.
instead of using sprintf. Is A really some sort of ASCII or hexadecimal number?

댓글 수: 6

Portgas Ace
Portgas Ace 2012년 9월 29일
because i have to separate an array to elements for some algorithms i have to do. for example 43 i need 4 and 3 separately.
Image Analyst
Image Analyst 2012년 9월 29일
편집: Image Analyst 2012년 9월 29일
Why not use str2double(A(1)), or str2double(A(2))? Nothing was wrong with the sprintf.
Portgas Ace
Portgas Ace 2012년 9월 29일
yup. that was what i did. btw, how do i assign values for A-F to 11-16? is the only way to do it is by if else statements?
Now, your A is letters instead of numbers. So do this:
A = 'ABCDEF'
for k = 1 : length(A)
theNumbers(k) = A(k) - 'A' + 11;
end
% Display:
theNumbers
In command window:
A =
ABCDEF
theNumbers =
11 12 13 14 15 16 17 18 19 10
Portgas Ace
Portgas Ace 2012년 9월 29일
why is there 17 18 19 10? it should be up to 16 only right? since up to F only?
Image Analyst
Image Analyst 2012년 9월 29일
Yes, you're right. It does really only go to 16. I didn't have a "clearvars" or "clear all" in there so there was left over stuff in theNumbers from prior test runs. This shows one good reason to use clear (though I know Jan doesn't like it) is to avoid goofs like this. Sorry about that.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by