Harsh Sharma
Harsh Sharma

Reputation: 65

Convert Cell to an Array

i have a cell array with dimensions 1x16384 consists of 16384 numbers. I tried using cell2mat(), but i get one element with all those numbers in it. How do i convert it to an array with 1x16384 dimension.

Assuming B= cell2mat(A);

A looks like;

Columns 16382 through 16384

    '15.849'    '16.337'    '14.872'

where as B looks like;

B =

-8.921-8.433-2.5745.23911.58714.02811.5876.7041.821-1.109-2.085-1.5970.3562.

console ; class(A), class(A{1}), size(A), size(A{1})

ans =

cell


ans =

char


ans =

           1       16384


ans =

     1     6

For mcve, Csv input file

DELIMITER = ' ';
HEADERLINES = 3;

% Import the file
newData1 = importdata('C:\Python34\co2a0000364.csv', DELIMITER, HEADERLINES);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end


new = textdata(6:16452,4);
A = new;

for z = 1:63
    A(i*257)=[];
end

B = A'
A= B;
A= cell2mat(B)
B= A

Upvotes: 0

Views: 79

Answers (1)

Hoki
Hoki

Reputation: 11792

You way of importing data is quite clumsy. I invite you to read the textscan function documentation, which allows much more flexibility in importing mixed data type (text and numeric data).

I propose you another way of importing, which uses textscan:

%% // Import only the 4th column of data
fid = fopen('co2a0000364.csv') ;
M = textscan( fid , '%*s%*s%*s%f' , 'Delimiter',' ' , 'HeaderLines',4) ;
fclose(fid) ;
M = cell2mat(M) ;

%% // reshape to have each channel in its own colum
M = reshape( M , 257 , [] ) ;

%% // Delete the channel number from the data table
M(1,:) = [] ;

This will give you a nice 256*64 matrix, containing the data for each of the 64 channels in one column.

if you really want them all in sequence in one column, just add:

M = M(:) ;

to the end of the code.


Explanation note: the textscan format specifier I use: '%*s%*s%*s%f' tells the function to read 4 elements per line:
- 3x strings : %s
- 1x floating point number: %f

adding the character * in the format specifier (for example %*s) for an element tells the function to ignore the element, so it does not put it into the output.

Upvotes: 2

Related Questions