Search code examples
arraysmatlabcell

Matlab cell arrays


Currently I have a 435x1 cell array that was formed with importdata. Each element is a string with 17 values separated by commas.

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.

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.

Can anyone tell me to turn this into a 435x17 cell array? 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. I want a 435x17 cell array where the columns are the strings delimited by commas.


Solution

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