zachd1_618
zachd1_618

Reputation: 4350

How to read a file with both decimal and hex/binary values in MATLAB

Say you have a simple text file which contains many columns. Some columns are decimal values already. Some are hexadecimal. Those columns are known.

test.txt >>

1.4 1.9 21 0030 0D12
0.3 3.3 91 FFFF 1111

I want to read the first three columns as decimal, and read the last two columns as hex.

Can this be done easily?

Thanks!

Upvotes: 0

Views: 836

Answers (2)

fgp
fgp

Reputation: 428

You can use fscanf. Are the hexadecimal values supposed to be all positive? If so, you may scan it like this:

fscanf(file_id,'%f %f %d %x %x')

Upvotes: 0

Eitan T
Eitan T

Reputation: 32930

It can easily be done by textread. For your example, just do:

[col1, col2, col3, col4, col5] = textread('myfile.txt', '%f %f %f %s %s');

This will read the decimal values normally, and treat the hexadecimal values as strings.

If you further wish to convert the strings into decimal values as well, use hex2dec with cellfun:

[col4, col5] = cellfun(@hex2dec, col4, col5);

Hope this helps.

Upvotes: 1

Related Questions