Azzera filtri
Azzera filtri

Convert very large nx1 cell into matrix mx3 matrix

2 visualizzazioni (ultimi 30 giorni)
Jon Smith
Jon Smith il 18 Feb 2016
Risposto: Wolfgang il 18 Feb 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:

Risposte (1)

Wolfgang
Wolfgang il 18 Feb 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

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by