regexp格式并使用两个单元阵列

时间:2021-10-21 15:57:42

I have a matrix made of 20x cells named T. each cell in this matrix contains the next thing:

我有一个由20x个细胞组成的矩阵叫做t。

1,26:00:01.490, 2.40,101.2,  7.8,-24.0,20.40,-0.76,-0.26

i would like to delimit and split it into columns so i used:

我想将它划分为几列,因此我使用:

Data=regexp(T,',','split');

The result is that im getting Data = 20x1 cells and inside each cell theres another cell with the splited vector i wanted.

结果是,我得到的数据= 20x1细胞,在每个细胞中都有一个我想要的分裂向量的细胞。

so far its all good but next thing i want to do is to take all of the time variables (26:00:01.490 for example) but they are inside 2 cells. The thing i wanted to do is Data{:,1}{1,2} but it says bad cell reference operation as an error.

到目前为止,一切都很好,但接下来我要做的是,取所有的时间变量(例如,26:06:1490),但它们都在2个单元内。我想做的是Data{:,1}{1,2}但是它说坏的单元格引用操作是错误的。

someone knows how to take whole column in cell array which is inside another cell?

有人知道如何在另一个单元格中获取整个列吗?

2 个解决方案

#1


1  

You can do it by concatenating the data between 1x9 cells by using a combination of vertcat and {:}.

您可以通过使用vertcat和{:}的组合来连接1x9单元之间的数据。

%//First form T
str='1,26:00:01.490, 2.40,101.2,  7.8,-24.0,20.40,-0.76,-0.26';
str=repmat({str},20,1);
Data=regexp(str,',','split');
Data1=vertcat(Data{:});

%//Get your time data in form of cells
timeData_cells=Data1(:,2);

%//Get your time data in form of a matrix
timeData_matrix=vertcat(Data1{:,2}) %//This will work only if all time strings 
                                    %//have the same length. 

%Answer
timeData_cells=
    '26:00:01.490'
    '26:00:01.490'
          .
          .
          .
    '26:00:01.490'
    '26:00:01.490'

timeData_matrix=
    26:00:01.490
    26:00:01.490
         .
         .
         .
    26:00:01.490
    26:00:01.490

#2


2  

If all the second-level cells have the same size (that is, all your strings have the same number of commas), you can vertically concatenate all the second-level cells to form a 2D cell array, and then it's easy to access columns.

如果所有的二级单元格都具有相同的大小(也就是说,您的所有字符串都具有相同数量的逗号),那么您可以垂直地将所有二级单元格串联起来,形成一个2D单元格数组,然后就很容易访问列。

For example:

例如:

>> T(1,:) = {'1,26:00:01.490, 2.40,101.2, 7.8,-24.0,20.40,-0.76,-0.26'}; %// example...
>> T(2,:) = {'2,26:00:01.490, 2.40,101.2, 7.8,-24.0,20.40,-0.76,-0.26'}; %// ...with 2 rows
>> Data = regexp(T,',','split'); %// as per your code
>> Data = vertcat(Data{:}); %// concatenate vertically to obtain 2D cell array
>> Data(:,1:2) %// now you can access columns
ans = 
    '1'    '26:00:01.490'
    '2'    '26:00:01.490'

#1


1  

You can do it by concatenating the data between 1x9 cells by using a combination of vertcat and {:}.

您可以通过使用vertcat和{:}的组合来连接1x9单元之间的数据。

%//First form T
str='1,26:00:01.490, 2.40,101.2,  7.8,-24.0,20.40,-0.76,-0.26';
str=repmat({str},20,1);
Data=regexp(str,',','split');
Data1=vertcat(Data{:});

%//Get your time data in form of cells
timeData_cells=Data1(:,2);

%//Get your time data in form of a matrix
timeData_matrix=vertcat(Data1{:,2}) %//This will work only if all time strings 
                                    %//have the same length. 

%Answer
timeData_cells=
    '26:00:01.490'
    '26:00:01.490'
          .
          .
          .
    '26:00:01.490'
    '26:00:01.490'

timeData_matrix=
    26:00:01.490
    26:00:01.490
         .
         .
         .
    26:00:01.490
    26:00:01.490

#2


2  

If all the second-level cells have the same size (that is, all your strings have the same number of commas), you can vertically concatenate all the second-level cells to form a 2D cell array, and then it's easy to access columns.

如果所有的二级单元格都具有相同的大小(也就是说,您的所有字符串都具有相同数量的逗号),那么您可以垂直地将所有二级单元格串联起来,形成一个2D单元格数组,然后就很容易访问列。

For example:

例如:

>> T(1,:) = {'1,26:00:01.490, 2.40,101.2, 7.8,-24.0,20.40,-0.76,-0.26'}; %// example...
>> T(2,:) = {'2,26:00:01.490, 2.40,101.2, 7.8,-24.0,20.40,-0.76,-0.26'}; %// ...with 2 rows
>> Data = regexp(T,',','split'); %// as per your code
>> Data = vertcat(Data{:}); %// concatenate vertically to obtain 2D cell array
>> Data(:,1:2) %// now you can access columns
ans = 
    '1'    '26:00:01.490'
    '2'    '26:00:01.490'