Converint between Cell arrays and Numbers and Strings
Show older comments
Hello,
I am trying to convert a double
cell array as shown to individual 8 bit cells. (I hope I have been able to frame the question correctly, if not please take a look at my screenshot)
cell array as shown to individual 8 bit cells. (I hope I have been able to frame the question correctly, if not please take a look at my screenshot)I want to convert each contents to individual bits, like for e.g [10000111] to individual 1 0 0 0 0 1 1 1
I have tried everything from cell2num, mat2cell, cell arrays and all but keep getting errors.
Please help me in solving this; also a little background theory on what these types of cells and 'cellarrays' mean would be a great help for next time!
Thanks,
Cheers,
kash022
2 Comments
madhan ravi
on 25 Jan 2019
Result=cellfun(@(x) x-'0',string(yourcellarray),'un',0)
"Converint between Cell arrays and Numbers and Strings"
"I am trying to convert a double cell array as shown..."
Nothing in your screenshot is related to cell arrays:
Nothing in your question is related to string arrays:
After using MATLAB for more than two years it would be a good idea to learn what the basic data classes are and how to identify them:
"also a little background theory on what these types of cells and 'cellarrays' mean would be a great help for next time!"
Why not try reading the MATLAB documentation?:
PS: this is not twitter. Please do not put ugly # symbols at the start of each tag.
Accepted Answer
More Answers (2)
per isakson
on 25 Jan 2019
Edited: per isakson
on 25 Jan 2019
"cell array as shown" The screenshot doesn't show any cell array
Another approach
>> str = sprintf('%d', 10000111 );
>> num = reshape( sscanf( str, '%1d' ), 1,[] )
num =
1 0 0 0 0 1 1 1
>>
4 Comments
Jan
on 25 Jan 2019
Easier:
str = sprintf('%d', 10000111 ) - '0'
per isakson
on 25 Jan 2019
Easier to type, but it's too much black magic to my taste.
Jan
on 25 Jan 2019
Exploiting the old-fashioned ASCII coding is less magic than calling sprintf, which is a huge library function. In very old Matlab versions, sprintf suffered from a bug, which allowed to gain admin privileges only by using strange format strings.
per isakson
on 25 Jan 2019
>> '€'+'ab' % undocumented behaviour(?)
ans =
8461 8462
>> "€"+"ab" % documented behaviour
ans =
"€ab"
Walter Roberson
on 25 Jan 2019
I do not think you have a cell array at present. I think you have a double array.
OutputCell = arrayfun(@(V) sprintf('%08d', V) - '0'), YourArrayNameGoesHere, 'uniform', 0)
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!