Urgent and small Matlab quistion

Status
Not open for further replies.

ahmad_abdulghany

Advanced Member level 4
Joined
Apr 12, 2005
Messages
1,206
Helped
102
Reputation
206
Reaction score
22
Trophy points
1,318
Location
San Jose, California, USA
Activity points
11,769
matlab fprintf binary %b

Assalamo alaykom
this an urgent and small matlab quistion ..

how can i use fprintf function or any other function to print binary numbers also how can i print anytype of numbers in certain length word ..

like that : fprintf( '%d',1)
i want it to print : 000000001 not 1 .. how can this is done ?

thank you ..
 

matlab function to print a binary number

thank you very much for these useful links ...

but sorry ... can you further help me to print numbers and make operation on them in binary format ?

thank you very much
 

dec2base в матлаб

Please clarify "make operation on them in binary format". "Binary" is a vague word. MATLAB does almost everything in binary internally. It converts the binary results to ASCII so humans can read them.

This will print what you requested, but it may not be what you intended:
fprintf( '%09d', 1)

If you want to convert an integer to base 2 string, then try this:
help dec2base
dec2base(1, 2, 9)
 
dec2base function in matlab

echo47 said:
Please clarify "make operation on them in binary format". "Binary" is a vague word. MATLAB does almost everything in binary internally. It converts the binary results to ASCII so humans can read them.

What i mean my dear is to print numbers in base 2 format !! i thought that it was very clear ! .. anyway .. i wanna do like this but not for decimal ...
fprintf('%b',9)
% assuming that there's a format specifier called %b for binary numbers and this returns 1001 on screen that's equivalent to 9 in decilmal

Thank you very much for your help
 

fprintfbinarymatlab

“Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime"
fprintf("%s",dec2base(9,2,4));
or something near to that. I can't be bothered to verify it.

Anyway, I guess you are smart enough to know what I'm insinuating, and I shall save it. Not trying to be overly harsh here, but just want you to know that there's a more important lesson to learn here rather than just how to express numbers in binary format. Hope this helps.
 
fprintf binary matlab

checkmate said:
“Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime"

First... Thank you for your help ..
second ... i wanted to tell here what i had learnt from you indirectly althogh you didn't mentioned what i asked about .. but i got more than what i want from you ..

i wanted to do a program like this :


for (k=1:15);
fprintf('%b',k);
end;



and i found that is wrong as there is nothing called %b

then what i did else is :


for (k=1:15);
fprintf('%s\n',dec2base(k,2,4));
end;


I hope this helps anyone..
thank you all
 

matlab course in cairo egypt?

No worries, friend! Sometimes questions are difficult to understand because we all come from different engineering backgrounds, and many of us speak different languages.

If you prefer the result in a 'char' array:
dec2base((1:15),2,4)
 
matlab fprintf binary

echo47 said:
If you prefer the result in a 'char' array:
dec2base((1:15),2,4)

thank you very much echo47 .. I am very happy as i found people that help me in let me think in matlab in that way .. really i am very very happy ..

now ... according to what you wrote .. this doesn't give result i want to print the binary numbers from 0000 to 1111 but it prints out characters from zeros and ones .. can you modify it such way that it prints out binary numbers .. like this :

0000
0001
0010
0011
....
1111

i am sure that you got it this time
 

matlab fprintf character array

This is an array of char arrays. So you would expect them to be expressed as
["0" "0" "0" "0"]
["0" "0" "0" "1"]
or something like that. It's correct.

So what you'll have to do is to feed each char array into the printf or fprintf statement with the %s identifier.
 

very small number in matlab

I did it like that :

fprintf('%c%c%c%c\n',dec2base((0:15),2,4))

and output was exactly as i wanna display ..

what if i wanna put each line of them in a variable?? .. for example :
x=0000 or 0001 .... etc ..

such that i can make use of it in another operations ..
 

=dec2base((i-1),2,4) =[] matlab

Let's say you have a integer variable x, say value 9. If you need to display it in binary form, you use the dec2base function. If you need to do operations on x, you use the x variable directly.
Of course, if you prefer doing it the hard way, you can always convert x into it's binary string equivalent, and then do the reverse (using the bin2dec function) to get the value 9.

All other string manipulation functions can be found from
**broken link removed**
 
Perhaps if you give us an example, or explain how you wish to use one-and-zeros notation, that may help us answer your question.

Are you hoping to type this: 00101111 + 01010110
and have MATLAB reply like this: 10000101 ?

This is about as close as you'll get using standard MATLAB functions:
Code:
dec2base(bin2dec('00101111') + bin2dec('01010110'), 2, 8)
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…