如何使用MATLAB在单元阵列中同时替换多个子串

时间:2014-03-14 23:17:27

标签: regex matlab

我被要求采用条形码符号序列,并用实际数字表示替换每个数字子串。换句话说,条形码序列中的数字'1''...::'表示。每个数字[0-9]由5个字符的符号表示。每个条形码由10位数组成,因此我想创建一个等同于符号条形码字符串的数字字符串。

离.-

'...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...'等同于1910499980

我想要完成的是捕获每个5个字符的序列,用它的数字等价替换它,然后继续下一个5字符序列。我到目前为止的代码如下,但它无法正常工作,它取代了一些符号,但不是全部。它也错误地替换了一些数字。我忘了提到每一行的第一个字符都要省略,所以我的一部分代码已经处理好了。

您可以提供的任何帮助都很棒,

谢谢, 丹


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

4 个答案:

答案 0 :(得分:2)

containters.Map课程就是为了这个目的:

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

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

使用values方法根据输入模式查找值:

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

将您的字符串细分为单元格,现在您可以翻译所有条形码值:

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))

答案 1 :(得分:0)

试试这个 -

<强>功能

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)';

生成

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

ans =

1910499980

答案 2 :(得分:0)

重塑字符串,使每组五个符号位于不同的行上,并使用ismember(使用'rows'选项)查找每行匹配的模式:

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

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

给出了

result =
1910499980

答案 3 :(得分:0)

使用初始条形码变量,模式变量和数字变量,此代码将读取条形码中的每一行。然后,它将单元格数组转换为字符数组,删除第一个字符,创建新的单元格数组,将单元格数组拆分为5个符号段,并用数字对应项替换每个段。最后一步是将新的数字字符串组合成一个类似于原始条形码数组的矩阵。

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);

输出:

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