0

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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.