MATLAB Answers

0

Conversion of 2000x3 decimal matrix .txt file into binary 2000x3 .txt file

Asked by Mert Genco on 13 Oct 2019
Latest activity Edited by per isakson
on 16 Oct 2019
To begin, I have 2000x3 matrix .txt file that contains decimal numbers within, so I want to convert all decimal values to binary representation of them. How can I do that in Matlab? Thanks in advance!
e.g
6 6 13
2 6 46
4 5 10
20 30 70
10 24 182
80 44 446
19 13 940
18 9 15
60 33 16
4 28 220
2 6 46
30 26 53
16 8 98
10 24 250
18 20 512
....

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephan
on 13 Oct 2019
Edited by Stephan
on 13 Oct 2019
 Accepted Answer

This should work:
A = readmatrix('test_input_xyz.txt');
[sz1, sz2] = size(A);
A = reshape(A,[],1);
B = sdec2bin(A,12);
B = reshape(string(B),sz1,sz2);
writematrix(B,'2_complement_values.txt','Delimiter','tab')
Note that i attached a .m-file which is needed. I loaded it down from here:
I checked one sample here:
which worked for me - but check results, since you work with a function from the web - not an inbuilt Matlab function.

  7 Comments

Okay, I attached the file and actually I want to write negative numbers in 2's complement form
This is it! I learned from this, really appreciated!

Sign in to comment.