将\t(tab)分隔的字符放入一个新的列向量中



背景

我有一个十六进制值的char数组,看起来像这样:

FF23    0008    FF58    00DE    00FC    0008
FF23    0008    FF58    00DF    0012    000C
FF23    0008    FF58    00DD    00F9    000C
FF23    0008    FF58    00DB    00F1    000B
....etc....

分隔符为\t(制表符)。我设法调整了char数组,使换行成为新行(即size(string) = numOfRows, 1)。

我需要将它们放入它们自己的4列中,以便绘制值。是的,hex2dec是我的目标,但它现在不会处理选项卡。

问题

给定上面的字符数组,如何将每组4个字符放入各自的列中?如果需要的话,我可以去掉标签并使用不同的分隔符。

我的糟糕尝试

xlsread('C:somefile', charArray)

这对我不起作用,因为我的电脑上没有Office,只有LibreOffice(http://tinyurl.com/kecqeg6)。

我试过了:

reshape(charArray, length(charArray), 6)

但这又回来了:

??? Error using ==> reshape
To RESHAPE the number of elements must not change.

我理解,但拜托。reshape()有点暗示你可以做这件事。

感谢您的帮助。我很感激。

编辑

之前我认为我有一个字符串向量,而不是一个字符数组。我已经更新了帖子以反映这一点。再次感谢。

这就是您想要的吗?

A = ['FF23 0008 FF58 00DE 00FC 0008'
'FF23 0008 FF58 00DF 0012 000C']; %// example data (char array)
B = regexprep(mat2cell(A,ones(1,size(A,1)),size(A,2)),'s',''); %// get rid of t
B = vertcat(B{:}) - '0'; %// turn B into a numeric matrix...
ind = B>=17;
B(ind) = B(ind) - 7; %// ... with values 0--15
B = reshape(permute(B,[2 3 1]),4,[],size(A,1)); %// reshape to make groups of 4
result = squeeze(sum(bsxfun(@times, B, 16.^(3:-1:0).'))).' %// convert each group

A为例,这给出了

result =
65315           8       65368         222         252           8
65315           8       65368         223          18          12
将文本文件转换为char数组是不必要的步骤。您可以直接从文本文件中进行操作。
fid = fopen('test.txt','r');    %read text file
C = textscan(fid,'%s%s%s%s%s%s', 'delimiter',' t','MultipleDelimsAsOne',1);   % 6 cols of chars
fclose(fid)
D = hex2dec(vertcat(C{:}));     % convert cells in strings, concatenate, then convert in doubles
D = reshape(D, numel(D)/6, 6);  % reshape the matrix

最新更新