2 views (last 30 days)

Hello,

I have two arrays a = [1 2 3 4 5 5] and b = [6 7 5 8 9]. I need to write them in CAN bit packing block with these Format

c = { [5] [6] [7] [8:23] [24:39] [40:55] } ( Number are just an example).

c is the combination between a and b. I've tried with this code :

c = unique([a b]);

strc = num2str(c);

splitc = strsplit(strc);

joinc = strjoin(splitc, ':');

strjoin ={joinc};

and it was wrong . At least how can I add a delimiter such as '[' and ']' in one element in my array? e.g. a = [1 2 3]

a1 =[1]--( it has to be string). Thanks for the help! :)

Renato Agurto
on 22 Dec 2015

Do you want something like this?

['[' strc(1) ':' strc(4) ']']

Guillaume
on 22 Dec 2015

Edited: Guillaume
on 22 Dec 2015

First, learn to read the documentation. The documentation that you linked clearly says that: "The data type [...] must be a MATLAB® cell array vector. [...] The cell array elements must be of type double array". There are no strings involved in this.

Secondly, I'm not even sure you've understood what the can bit packing function does. The bit-pattern is not the same at all as the data you want to transmit. There are many bit patterns that could be valid for transmitting a = [1 2 3 4 5 5]. The following bit-pattern is a possiblity: c = {0, 1:2, 3:4, 5:7, 8:10, 11:13)}, that's the minimum pattern to transmit a but will not transmit b = [2 2 3 4 5 5] as there is not enough bits to transmit the first number. Equally valid pattern is: c = {0:9, 10:19, 20:30, 31:41, 42:52, 53:63}.

The bit-pattern is defined by:

- how many numbers you want to pack into a double
- the maximum value of each of these numbers

Given the maximum values you want to transmit

maxvalues = [2 2 6 4 10 8]; %for example.

You can calculate the smallest bit-pattern with:

bitsrequired = floor(log2(maxvalues)) + 1;

endbits = cumsum(bitsrequired) - 1;

assert(endbits(end) <= 63, 'more bits are required than can fit into a double');

bitpattern = arrayfun(@(s, e) (s:e), [0, endbits(1:end-1)+1], endbits, 'UniformOutput', false)

Guillaume
on 22 Dec 2015

Following onto the comment you wrote to Renato's answer while I was typing mine, if given inputs

startbits = [0 8 16];

endbits = [7 8 16];

To generate the corresponding bit pattern, you'd simply do:

bitpattern = arrayfun(@(s, e) s:e, startbits, endbits, 'UniformOutput', false);

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331053

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331053

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331064

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331064

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331153

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331153

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331155

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/261249-how-can-i-do-this-5-6-7-8-23-24-39-40-55-these-number-are-just-an-example#comment_331155

Sign in to comment.