Borrar filtros
Borrar filtros

Convert very large nx1 cell into matrix mx3 matrix

1 visualización (últimos 30 días)
Jon Smith
Jon Smith el 18 de Feb. de 2016
Respondida: Wolfgang el 18 de Feb. de 2016
I have a large cell of size 190004x1. Each cell consists of a number, a decimal point (.), a minus sign (-) or a blank space.
I want to merge each cell element without a space between them into a single cell, whenever a space occurs I want to move to the cell on the right. Whenever a I have 2 spaces I want to start a new line. Here is an example:

Respuestas (1)

Wolfgang
Wolfgang el 18 de Feb. de 2016
Hi.
First: find out if all of your cell elements are of class type 'char'. Perhaps you have a mix of 'char' and 'double' / 'integer' elements.
If there are not only 'char' elements, you can fix this with:
% Convert all elements in your cell into type 'char'
% (only if you have mixed class types in your cell )
newCell = cellfun(@num2str, yourCell);
As result (newCell) you get a very long character array (string) of the size [nx1]; Now you can split the transposed character array at the delimiter (blank spaces) and convert it into double values
% split the transposed character array at the delimiter (blank spaces)
splittedStrings = strsplit(newCell',' ');
% convert the strings to double values
numbers = str2double(splittedStrings)
Now, you have long vector of numbers. Bring them into matrix form with the reshape command:
% reshape vector to matrix
yourMatix = reshape(numbers,[],3);
I hope this will help you

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by