Convert space separated string table to cell?

Hello :)
I can't seem to figure out how to convert a string table without using a (textscan etc.) loop,
from :
Table = ['A2 6C 33 04 00 81 00 80';'3F 11 65 01 0A '];
to :
Result = {'A2' '6C' '33' '04' '00' '81' '00' '80';'3F' '11' '65' '01' '0A' ' ' ' ' ' '};
Please note that "Table" is fixed-width Nx23 which could simplify things.
FYI, the end-goal is to convert from hex to decimal to cell-table:
Dec = [162 108 51 4 0 129 0 128; 63 17 101 1 10 0 0 0];

1 commentaire

José-Luis
José-Luis le 17 Juin 2014
Modifié(e) : José-Luis le 17 Juin 2014
Do you need to distinguish between a zero caused by a space and the string 00?

Connectez-vous pour commenter.

 Réponse acceptée

Cedric
Cedric le 17 Juin 2014
Modifié(e) : Cedric le 17 Juin 2014
>> Dec = reshape( hex2dec( regexp( reshape( Table', 1, [] ), '..\s?', ...
'match' )), 8, [] )'
Dec =
162 108 51 4 0 129 0 128
63 17 101 1 10 0 0 0

Plus de réponses (3)

Table = ['A2 6C 33 04 00 81 00 80';'3F 11 65 01 0A ']
Table(:,3:3:end)=[];
[n,m]=size(Table);
[bb,aa]=meshgrid(1:2:m,1:n);
out=arrayfun(@(x,y) hex2dec(Table(x,y:y+1)),aa,bb)

3 commentaires

dpb
dpb le 17 Juin 2014
Very clever, Azzi...I was trying to puzzle out how to get the indices for arrayfun-- meshgrid never crossed my mind here.
Had added the trailing blank to regularize the array; was thinking if could linearize then reshape back and decided to refresh as somebody was bound to have had the bright idea...
Thank you soo much Azzi, very clever :)
However, even though I didn't explicitly mention it, I need it to be scalable. When I use a larger "Table" (60000x23 char) arrayfun takes a very long time to finish.
Instead, by passing a 60000x8 cell hex2dec is very fast. The problem is that I can't figure out how to construct the cell.
As reference, please try the below to see how much faster it is:
Cells = {'A2' '6C' '33' '04' '00' '81' '00' '80';'3F' '11' '65' '01' '0A' ' ' ' ' ' '};
Cells = repmat(Cells, 30000, 1); % Create large cell
out = uint8(hex2dec(Cells)); % Convert hex to dec
out = reshape(out, 60000, 8); % Reshape to desired shape
Table = ['A2 6C 33 04 00 81 00 80';'3F 11 65 01 0A ']
Table(:,3:3:end)=[];
[n,m]=size(Table);
[bb,aa]=meshgrid(1:2:m,1:n);
out=arrayfun(@(x,y) Table(x,y:y+1),aa,bb,'un',0)

Connectez-vous pour commenter.

Andrei Bobrov
Andrei Bobrov le 17 Juin 2014
Modifié(e) : Andrei Bobrov le 17 Juin 2014
nn = size(Table,1);
a = cellfun(@(x)regexp(x,'\w*','match'), num2cell(Table,2),'un',0);
n = cellfun(@numel,a);
mm = max(n);
m = mm - n;
b = arrayfun(@(x)[hex2dec(a{x});zeros(m(x),1)]', (1:nn)','un',0);
out = cat(1,b{:});
Azzi Abdelmalek
Azzi Abdelmalek le 17 Juin 2014
Modifié(e) : Azzi Abdelmalek le 17 Juin 2014
Table = ['A2 6C 33 04 00 81 00 80';'3F 11 65 01 0A ']
[n,m]=size(Table)
Table(:,3:3:end)='/';
ss=regexp(num2cell(Table,2),'/+','split')
out=cellfun(@hex2dec,reshape([ss{:}],[],n)')

2 commentaires

Bjoern
Bjoern le 17 Juin 2014
Awesome, I really appreciate it!
Comparing this solution to Cedric Wannaz below, his is actually about 3 times faster so I will have to pick that one as "the answer". Thanks again!
Cedric
Cedric le 17 Juin 2014
Careful, "faster" is not always "better"! My solution assumes that you know a priori the max width in terms of number of columns.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by