简体   繁体   English

Matlab单元阵列

[英]Matlab cell arrays

Currently I have a 435x1 cell array that was formed with importdata. 目前我有一个由importdata组成的435x1单元阵列。 Each element is a string with 17 values separated by commas. 每个元素都是一个由逗号分隔的17个值的字符串。

I want to have the strings fill up the columns of each row, rather than have one string per row contained in one element. 我想让字符串填满每行的列,而不是每行包含一个字符串。 So basically, I want a 435x17 cell array where each element is one cell array, rather than a 435x1 cell array with each element is a string. 所以基本上,我想要一个435x17的单元阵列,其中每个元素是一个单元阵列,而不是435x1单元阵列,每个元素都是一个字符串。

The closest I have gotten so far is: 我到目前为止最接近的是:

data = importdata('my_data.data');
for x=1:size(data,1)
    data(x,:) = {strsplit(data{x}, ',')};
end

This will make the string at each element a cell array. 这将使每个元素的字符串成为单元格数组。 With the above code, I still have a 435x1 cell array, but each element is now a 1x17 cell array. 使用上面的代码,我仍然有一个435x1单元阵列,但每个元素现在是1x17单元阵列。

Can anyone tell me to turn this into a 435x17 cell array? 任何人都可以告诉我把它变成一个435x17的单元阵列吗? Any help is appreciated. 任何帮助表示赞赏。

A row from the data file looks like: 数据文件中的一行如下所示:

republican,n,y,n,y,y,y,n,n,n,y,?,y,y,y,n,y

There are 435 rows of those strings. 这些字符串有435行。 I want a 435x17 cell array where the columns are the strings delimited by commas. 我想要一个435x17的单元格数组,其中列是用逗号分隔的字符串。

You can do this using cat . 你可以用cat做到这一点。 Here's a smaller example: 这是一个较小的例子:

data = {{1,2,3};{4,5,6};{7,8,9};{10,11,12}};
size(data)
ans =

   4   1

newData = cat(1,data{:});
size(newData)
ans =

   4   3

EDIT: If you have a cell array of strings instead, you can do the following: 编辑:如果您有一个字符串的单元格数组,您可以执行以下操作:

data = {{'123'};{'456'};{'789'};{'abc'}};
size(data)
ans =

   4   1

data2 = cat(1, data{:});
data3 = cell2mat(newData);
size(data3)
ans =

   4   3

data3 =

123
456
789
abc

Try something like this: 尝试这样的事情:

temp_data = importdata('my_data.data');
data = cell(size(temp_data,1),17);
for x=1:size(temp_data,1)
    data(x,:) = strsplit(temp_data{x}, ',');
end
clear temp_data;

Do you have any sample data? 你有样品数据吗? Perhaps just a few rows of the csv file? 也许只是几行csv文件?

Regardless, if you have R2013b you should be able to do this using a table. 无论如何,如果您有R2013b,您应该可以使用表格来完成此操作。 For the 'data.csv' file: 对于'data.csv'文件:

republican,n,y,n,y,y,y,n,n,n,y,?,y,y,y,n,y
democrat,y,n,y,n,n,n,y,y,y,n,!,n,n,n,y,n

You can then import this into matlab using a table : 然后,您可以使用将其导入matlab:

>> t = readtable('data.csv','ReadVariableNames', false)

t = 

        Var1        Var2    Var3    Var4    Var5    Var6    Var7    Var8    Var9    Var10    Var11    Var12    Var13    Var14    Var15    Var16    Var17
    ____________    ____    ____    ____    ____    ____    ____    ____    ____    _____    _____    _____    _____    _____    _____    _____    _____

    'republican'    'n'     'y'     'n'     'y'     'y'     'y'     'n'     'n'     'n'      'y'      '?'      'y'      'y'      'y'      'n'      'y'  
    'democrat'      'y'     'n'     'y'     'n'     'n'     'n'     'y'     'y'     'y'      'n'      '!'      'n'      'n'      'n'      'y'      'n' 

That is how you can easily create a table. 这就是您可以轻松创建表格的方法。 I would also suggest looking into just using a table to hold this data instead of processing it as a cell array, as it is made to handle data processing tasks and has features that may help with what you are trying to do. 我还建议只考虑使用一个表来保存这些数据,而不是将其作为一个单元阵列处理,因为它是为处理数据处理任务而设计的,并且具有可能有助于你尝试做什么的功能。 However, if you really need it to be a cell array you can certainly just convert the table to a cell: 但是,如果你真的需要它是一个单元格数组,你当然可以将表转换为一个单元格:

>> c = table2cell(t)

c = 

    'republican'    'n'    'y'    'n'    'y'    'y'    'y'    'n'    'n'    'n'    'y'    '?'    'y'    'y'    'y'    'n'    'y'
    'democrat'      'y'    'n'    'y'    'n'    'n'    'n'    'y'    'y'    'y'    'n'    '!'    'n'    'n'    'n'    'y'    'n'

>> size(c)

ans =

     2    17

Hope that helps! 希望有所帮助!

If you do not have R2013b, or do not want to leverage table for some reason, you can also do this like so: 如果您没有R2013b,或者由于某种原因不想利用表,您也可以这样做:

cellData = cell(numel(data), 17);  % preallocate
for idx=1:numel(data)
    cellData(idx,:) = strsplit(data{idx}, ',');
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM