user3421802
user3421802

Reputation: 19

How can I replace multiple substrings simultaneously within a cell array using MATLAB

I have been asked to take a barcode symbol sequence, and replace each number substring with the actual number representation. In other words, a number '1' in the barcode sequence is represented by '...::'. Each number [0-9] is represented by a 5 character symbol. Each barcode is made up of 10 digits, so I want to create a number string that is equivalent to the symbolic barcode string.

ex.-

'...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...' would be equivalent to 1910499980

What I am trying to accomplish is to capture each 5 character sequence, replace it with its numeric equivalent, then move on to the next 5 character sequence. The code I have so far is below, but it does not function correctly, it replaces some of the symbols, but not all. It also substitutes some of the numbers incorrectly. I forgot to mention that the first character of each line is to be omitted, so a portion of my code has already taken care of that.

Any help you can provide would be great,

Thanks, Dan


barcodes = {':...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...';
':::...:..:...::...::.::.....:.:.:..:..::...:.:..:.:';
'::.:...:..:.::..::...:...:::.....::...::.::...:..:.';
'::...:.:..:::...:...::..:...:.:..::.:..:..:..::...:';
':::....::...:.:...:.:::...:.:..:.:..::...::...:.:..';
':.:..:..::...:.:...::::......:::...:.:..:.::..:...:';
'::.:..:..:....::..:.:..:.:.:..:..::..:.:.:..:.:..:.';
':.::.....:::...:.::.....::..::..:..:.:..:.:..:.:..:';
'::...::..:..::...::...::..:...:.:.:.:...:.:.:...::.';
'::..:...::...:.:::......:::..:...::..:..:..:.::.:..';
'::..:...:.:::...:...:...::.:..:.:..:.:..:.:..:.::..';
':.:..::..:.:..:...:.:.:..::.:..:.:..:.:..:..:.:.:..';
':..::...:.:..::.::....::....::..:..:..:.:.:.:...:.:';
':..::..::.....::::....:..:.:.:.:.:..:..:...::....::';
':.::...::...:.:.::....::....:.:::.....:.:..::.:...:';
'::..:..:..:..::...:.:..:.:.:..:..::.:.:...:..:...::';
':.:.:.:...:::...::...:...:..::..:..:..:.:.:..::..:.';
':...::..:.:..:.:.:..:.::....::.:..:.:..:.:..:.:..:.';
':..:.:..::.:..:.::....::..:...:.:..:..::...:.::...:';
':.:..::...::..:.::...:.:....::.:..:...:.:...:::..:.';
'::..:..:.:.::...::....:..:.:..:..::..:.:..:..::...:';
'::..:.::....:.:...:.:..::.:..:..:..:..::...:.:.:.:.';
':.:.:..::..::...::......:::..:..:..:.:.:..:.:..::..';
':..::.:...:..:.:::...:.:.....::..:.:...::..::...:.:';
':.:..:::....::..::......::.::...::...:.:..:..::..:.';
':..:.::...:.::..:.:...:.:..:..:..::...:.::.:....::.';
':.::...:.:.:..:.:.:..:...:.:..::.:....::...:.::...:';
':..::.::.....::.::.....::.:..:..:..::...:.:..::..:.';
':::.....:.:...::..::.:.:..::...::...::...::....:.:.';
':::...:..:..:.:..:..:::....:..:.:.:..:.:...::.:...:';
':::.....:.::.:..::.....::..:..:..::...:.::.:..:..:.';
'::..:.:.:..::...::.....:.:.::..::....::..::...:.:..';
':.:.:.::...::......::...::..::..:..:.:..:..:.:::...';
':.:.:...::.:...:::....::..:.:....::...:.:...::.:..:'};

pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
num1 = '1';
num2 = '2';
num3 = '3';
num4 = '4';
num5 = '5';
num6 = '6';
num7 = '7';
num8 = '8';
num9 = '9';
num0 = '0';

patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];
numbers = [num1;num2;num3;num4;num5;num6;num7;num8;num9;num0];

eachString = [];
barcodeMinusFirst = [];

    for i = 1:length(barcodes);
        eachString = [eachString; cellstr(barcodes(i))]; 
    end

barcodeStrings = char(eachString);
barcodeStrings = barcodeStrings(:,2:end);
barcodeStrings = cellstr(barcodeStrings);
patternCell = cellstr(patterns);
numberCell = cellstr(numbers);

    for i = 1:length(barcodes)
    newBarcodes = regexprep(barcodeStrings, patternCell, numberCell);
    end

Upvotes: 2

Views: 860

Answers (4)

Luis Mendo
Luis Mendo

Reputation: 112669

Reshape your string so that each group of five symbols is on a different row, and use ismember (with the 'rows' option) to find which pattern is matched by each row:

str = '...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...'; %// example string
%// Variables "patterns" and "numbers" are defined in your code

[~, ind] = ismember(reshape(str,5,[]).', patterns, 'rows');
result = numbers(ind).';

which gives

result =
1910499980

Upvotes: 0

user3421802
user3421802

Reputation: 19

Using the initial barcodes variable, patterns variable, and numbers variable, this code will read through each line in barcodes. It will then convert the cell array to a character array, remove the first character, create a new cell array, split the cell array into 5 symbol segments, and replace each segment with its numeric counterpart. The final step is to combine the new numeric strings into a matrix similar to the original barcodes array.

barcodes = {':...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...';
':::...:..:...::...::.::.....:.:.:..:..::...:.:..:.:';
'::.:...:..:.::..::...:...:::.....::...::.::...:..:.';
'::...:.:..:::...:...::..:...:.:..::.:..:..:..::...:';
':::....::...:.:...:.:::...:.:..:.:..::...::...:.:..';
':.:..:..::...:.:...::::......:::...:.:..:.::..:...:';
'::.:..:..:....::..:.:..:.:.:..:..::..:.:.:..:.:..:.';
':.::.....:::...:.::.....::..::..:..:.:..:.:..:.:..:';
'::...::..:..::...::...::..:...:.:.:.:...:.:.:...::.';
'::..:...::...:.:::......:::..:...::..:..:..:.::.:..';
'::..:...:.:::...:...:...::.:..:.:..:.:..:.:..:.::..';
':.:..::..:.:..:...:.:.:..::.:..:.:..:.:..:..:.:.:..';
':..::...:.:..::.::....::....::..:..:..:.:.:.:...:.:';
':..::..::.....::::....:..:.:.:.:.:..:..:...::....::';
':.::...::...:.:.::....::....:.:::.....:.:..::.:...:';
'::..:..:..:..::...:.:..:.:.:..:..::.:.:...:..:...::';
':.:.:.:...:::...::...:...:..::..:..:..:.:.:..::..:.';
':...::..:.:..:.:.:..:.::....::.:..:.:..:.:..:.:..:.';
':..:.:..::.:..:.::....::..:...:.:..:..::...:.::...:';
':.:..::...::..:.::...:.:....::.:..:...:.:...:::..:.';
'::..:..:.:.::...::....:..:.:..:..::..:.:..:..::...:';
'::..:.::....:.:...:.:..::.:..:..:..:..::...:.:.:.:.';
':.:.:..::..::...::......:::..:..:..:.:.:..:.:..::..';
':..::.:...:..:.:::...:.:.....::..:.:...::..::...:.:';
':.:..:::....::..::......::.::...::...:.:..:..::..:.';
':..:.::...:.::..:.:...:.:..:..:..::...:.::.:....::.';
':.::...:.:.:..:.:.:..:...:.:..::.:....::...:.::...:';
':..::.::.....::.::.....::.:..:..:..::...:.:..::..:.';
':::.....:.:...::..::.:.:..::...::...::...::....:.:.';
':::...:..:..:.:..:..:::....:..:.:.:..:.:...::.:...:';
':::.....:.::.:..::.....::..:..:..::...:.::.:..:..:.';
'::..:.:.:..::...::.....:.:.::..::....::..::...:.:..';
':.:.:.::...::......::...::..::..:..:.:..:..:.:::...';
':.:.:...::.:...:::....::..:.:....::...:.:...::.:..:'};
pat1 = '...::';%patterns corresponding to each barcode sequence
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
num1 = '1'; % variables for each number
num2 = '2';
num3 = '3';
num4 = '4';
num5 = '5';
num6 = '6';
num7 = '7';
num8 = '8';
num9 = '9';
num0 = '0';
split = [];

patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0]; %character matrix for comparison
numbers = [num1;num2;num3;num4;num5;num6;num7;num8;num9;num0]; %character matrix for replacement

patternCell = cellstr(patterns); %convert patterns to cell array
numberCell = cellstr(numbers); %convert numbers to cell array

barcodeStrings = char(barcodes); %convert to character array
barcodeStrings = barcodeStrings(:,2:end);
barcodeStrings = cellstr(barcodeStrings);

for i = 1:length(barcodes)
    newBarcodes = regexp(barcodeStrings, '\W{1,5}', 'match');
end

for j = 1:length(newBarcodes)
    split = [split; regexprep(newBarcodes{j}, patternCell, numberCell)];
end

numbMat = cell2mat(split);

output:

disp(numbMat)
1910499980
0833024322
9460703308
7407823847
0652099009
4321017467
9812243588
6176134444
7866675753
8320183429
8207144446
4882499989
3230634252
3610459831
6650620237
8432243941
5700734248
1224638888
2380674327
4780938218
8500443547
8052384325
5600184556
3720912132
4060166548
2769543293
6589749327
3030384748
0213900005
0854045537
0290343298
8900260609
5001134420
5370693214

Upvotes: 0

chappjc
chappjc

Reputation: 30579

The containters.Map class would serve this purpose:

>> nums = [1:9 0];
>> patterns = {'...::';'..:.:';'..::.';'.:..:';'.:.:.';...
             '.::..';':...:';':..:.';':.:..';'::...'};
>> map = containers.Map(patterns,vals)

map = 
  Map with properties:
        Count: 10
      KeyType: char
    ValueType: double

Use the values method to look up a value based on an input pattern or patterns:

>> codeTest = patterns([5 4 7 1]);
>> map.values(codeTest)'
ans = 
    [5]    [4]    [7]    [1]

Segment your strings into cells and now you can translate all your barcode values:

bc = cell2mat(barcodes); bc = bc(:,2:end);
codes5 = mat2cell(bc,ones(size(bc,1),1),5*ones(1,size(bc,2)/5));
codesNumeric = cell2mat(map.values(codes5))

Upvotes: 2

Divakar
Divakar

Reputation: 221554

Try this -

Function

function output_string = barcode_seq(input_string)

%%// Params
pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];

barcode_len = size(patterns,2);

s1 = reshape(input_string,barcode_len,[])';
comp_exp_mat  = bsxfun(@eq,patterns,permute(s1,[3 2 1]));
[x,y] = find(squeeze(sum(comp_exp_mat,2))==barcode_len);
x(x==10)=0;
output_string = num2str(x)';

Run

>> barcode_seq('...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...')

ans =

1910499980

Upvotes: 0

Related Questions