Re: cell array to normal array




"Eszter " <gulacsy@xxxxxxxxxxx> schrieb im Newsbeitrag
news:fg9s3s$fi7$1@xxxxxxxxxxxxxxxxxxxxx
I am wondering if anyone could help me with this as I
tried searching to the solution to this problem on Matlab
Central, but no success. Tried cell2num, but it didn't
work.

I have a cell array with each cell containing a string of
four numbers of varying length, e.g. '434.23 4567.7
312 34567.98' I have about 400 rows of this type of
data - four numbers with varying significant digits.

I concluded from the help file that I could create a
variable from each cell of the cell array, but doing that
four 400 rows is tedious, so I am wondering if there is an
easier way to convert this cell array to a matrix,
containing 400 rows with a string of numbers in each row.
Cell2mat doesn't seem to work as the strings in the cells
of the cell array are not all the same length.

Thanks in advance for any help.

Eszter



Hi Eszter,

sometimes a loop is the easiest way to do things like that:
say x is a cell array of length 400, then:

m = zeros(400, 4);
for i=1:400
m(i,:) = str2num(x{i});
end

gives you a 400x4 matrix. But if you prefer not to use loops:
mm = cellfun(@(v) str2num(v)', x, 'UniformOutput', false);
m = [mm{:}]';
gives the same result...

Titus


.



Relevant Pages

  • Re: gui - appending text
    ... 'Hello' in a newline in txtBox. ... when you setthe String ... property of a text box or edit box with a char array that has a newline ... Another poster suggested using cell array of strings instead. ...
    (comp.soft-sys.matlab)
  • Re: write string to file
    ... After each string, it puts again a square. ... of a function commonly used to create a cell array of a certain size. ... that could be a vector, matrix, or array I would use NUMEL. ...
    (comp.soft-sys.matlab)
  • Re: "for n in x"
    ... Those first two lines of the 'for' loop are okay, but it would be nice just ... also to avoid the creation of that 'idx' variable which really exists only to ... means that aList is a list which in MATLAB is called a cell array. ... % item is a 1x1 cell array, not the actual string contents. ...
    (comp.soft-sys.matlab)
  • Re: define string and structure
    ... zeros function is the numeric, not string. ... instead of parentheses, when indexing into the cell array. ... extract a smaller cell from the cell array; ...
    (comp.soft-sys.matlab)
  • Re: Replacing strings with numbers using textscan
    ... and then using strcmp ... I have a file of cell array type that I have read using TEXTSCAN. ... Cis type string and Care of type double ... I would like to sort the rows of the file according to the column ...
    (comp.soft-sys.matlab)