1

I am extracting some data form a file, including strings like:

n ={[1,1] = 0:7:80:bc:eb:64  
    [2,1] = 0:7:80:bc:eb:69
    [3,1] = 0:7:80:bc:eb:69
    [4,1] = 0:7:80:bc:eb:69
   }

I need to change the '0' to '00', and the '7' to '07'. Then use the function hex2dec to convert it into decimal numbers.

I'm using the following code:

r=strrep(0:7:80:bc:eb:69 , ':', ''); 
m= hex2dec(r)

Maybe there is a better way to do it?

2 Answers 2

3

You can just split each string on : using strsplit. That gives a cell array of strings (char vectors) that you can directly pass to hex2dec; no need for zero-padding:

n = {'0:7:80:bc:eb:64';
     '0:7:80:bc:eb:69';
     '0:7:80:bc:eb:69';
     '0:7:80:bc:eb:69'}; % data: cell array of strings
k = 1; % select one cell
t = strsplit(n{k}, ':');
result = hex2dec(t);

This gives

t =
    '0'    '7'    '80'    'bc'    'eb'    '64'

result =
     0
     7
   128
   188
   235
   100

To get the numbers from all strings as a matrix, join the strings of the cell array using strjoin, apply the above, and then apply reshape:

n = {'0:7:80:bc:eb:64';
     '0:7:80:bc:eb:69';
     '0:7:80:bc:eb:69';
     '0:7:80:bc:eb:69'}; % data: cell array of strings
nj = strjoin(n, ':');
t = strsplit(nj, ':');
result = hex2dec(t);
result = reshape(result, [], numel(n)).';

This gives

result =
     0     7   128   188   235   100
     0     7   128   188   235   105
     0     7   128   188   235   105
     0     7   128   188   235   105
Sign up to request clarification or add additional context in comments.

8 Comments

When I do it, a message error appear: error: strsplit: S and DEL must be string values. I think this is because the format of the file i extract doesn't match.. Here is the file extracted: n = { [1,1] = 0:7:80:bc:eb:64 [2,1] = 0:7:80:bc:eb:69 [3,1] = 0:7:80:bc:eb:69 [4,1] = 0:7:80:bc:eb:69 }
@Mbdt Did you try replacing s with n{1}?
@beaker Good idea. I hadn't seen the updated question. Edited
Thanks it works!! and is there a function allowing me to make the sum of these values ? @LuisMendo
The sum of the result vector? Use sum(result)
|
3

strsplit and hex2dec work fine as the above answer suggested. I am providing a simpler and faster solution by sscanf:

n = {'0:7:80:bc:eb:64';
 '0:7:80:bc:eb:69';
 '0:7:80:bc:eb:69';
 '0:7:80:bc:eb:69'}; % data: cell array of strings

t = sscanf(n{1}, '%x:')'

t =

 0     7   128   188   235   100

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.