Need to Split a column of type 'string' in a Table in to group of 4 characters giving new names to the result.

13 ビュー (過去 30 日間)
I have read a csv file in to a table in below format of large number of rows. The Data field is of type string.
Time Identifier Data
00:06:40.23 "300" "65 00 69 00 6D 00 75 00 "
00:06:40.25 "100" "B7 FF E5 FF 7D 10 01 00 "
I need to split the Data column in to 4 columns with new names. i.e. as below
Time Identifier AC1 AC2 AC3 AC4
00:06:40.23 "300" "6500" "6900" "6D00" "7500"
00:06:40.25 "100" "B7FF" "E5FF" "7D10" "0100"
  1 件のコメント
manoj hanu
manoj hanu 2019 年 8 月 11 日
I tried first removing all the spaces with strrep.
A = strrep(table.Data, ' ', '');
Now got a string array in A without the spaces. Is there a better way in which this can be divided in to 4 columns now??
A = 2x1 string array
"650069006D007500"
"B7FFE5FF7D100100"

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 11 日
編集済み: Bruno Luong 2019 年 8 月 11 日
s=[ "65 00 69 00 6D 00 75 00 ";
"B7 FF E5 FF 7D 10 01 00 "]
c = char(s);
c(:,3:3:end)=[];
ssplit = string(mat2cell(c,ones(1,size(c,1)),4*ones(1,4)))
Result
ssplit =
2×4 string array
"6500" "6900" "6D00" "7500"
"B7FF" "E5FF" "7D10" "0100"
  1 件のコメント
manoj hanu
manoj hanu 2019 年 8 月 15 日
Thank you for the answer. With your code I used
cell2table(ssplit, 'VariableNames' , { 'AC1' 'AC2' 'AC3' 'AC4'});
to get the columns named.

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 8 月 11 日
v=regexp(strrep(T.Data,' ',''),'\w{4}','match');% naming a table with a variable name table is a terrible idea (will hinder the in-built function table())use T for example
AC = vertcat(v{:});
AC = array2table(AC);
T.Data = [];
T = [T,AC]

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by