Introduction to MATLAB cell arrays

Source: Internet
Author: User

Reprint: http://www.matlabsky.com/forum.php?mod=viewthread&tid=21089

Due to the need for work, a large number of experimental data are often processed. The basic is from the instrument to export, with the progress of social development, the amount of people to obtain data is growing, many people are, is now a huge data age ...

Speaking from the cell use
When reading a file, the cell array (all kinds of translations have, cell arrays, array of cells ...). Direct disregard) is the darling of MATLAB, the basic will appear, the long-term use of the discovery frequency than the struct a lot more ~ either import data or use Textscan to obtain data. From the perspective of long-term use of high-level language, especially accustomed to object-oriented I, More accustomed to using struct arrays, the concept is very similar, but the reality is ... Let's look at the definition of MATLAB in Help:

a cell array is a collection of containers called cells in which you can store different types of data.


The essence is that you can store different types of data . It can be a type of MATLAB or a custom type.

Some operations of the cell array

  • Create: Use {} and cell (...) directly form, another subscript method can be assigned. Note that the latter two can be provisioned with memory, the memory is space continuous ~
  • Read content: {subscript} and (subscript) The difference is that the type () is the cell array, {} is the actual type. The results are consistent.
    1. >> a={' Ah ', 123,[3 5 6],[1 2;7 8],sym (1)}
    2. A =
    3. ' ah ' [123] [1x3 double] [2x2 double] [1x1 sym]
    4. >> class (A{1})
    5. Ans =
    6. Char
    7. >> Class (A (1))
    8. Ans =
    9. Cell
    10. >>
    11. c{5,3}{4,7} (:, 4)
    12. Interpretation: An element of the cell array is a cell type and contains a common matrix type
    13. x = c{5,3}; % x is a cell array
    14. y = x{4,7}; % y is also a cell array
    15. z = y (:, 4) % z is a standard array
    Copy Code
  • What to adjust: Add and remove consistent with common matrix methods
  • Related functions:
  • Celldisp: Show All content
  • Cell: Creating an empty array of cells
  • Cellplot: Displaying content graphically
  • Cell2mat: Transforming an array into a normal matrix
  • Mat2cell: Transforming a numerical matrix into a cell array
  • Num2cell: Converting a numeric array into a cell array
  • Cell2struct: Transforming an array into a structure
  • Struct2cell: Transforming a structure into a cell array
  • Iscell: Determines whether the input is a cell array
  • Cellfun: Executes the specified function for each cell of the cell array the fun can be a special function or handle
    1. Days{1} = ' Sunday '; Days{2} = ' Monday ';
    2. Days{3} = ' Tuesday '; days{4} = ' Wednesday ';
    3. DAYS{5} = ' Thursday '; Days{6} = ' Friday ';
    4. Days{7} = ' Saturday ';
    5. Shortnames = Cellfun (@ (x) x (1:3), days, ' Uniformoutput ', false)
    6. Shortnames =
    7. ' Sun ' Mon ' Tue ' Wed ' Thu ' Fri ' Sat '
    Copy Code
  • Deal: Assigning input parameters to output [Y1, Y2, Y3, ...] = Deal (x{:}), can be simplified [a,b,c,d] = c{:}

type conversion of a cell array See the above types of conversion functions, maybe you will laugh, feel nothing special, but the use of this cell array of friends, most of the estimated will have the experience of conversion failure-_-very pit dad ...
Cell array and Struct array

    • s = cell2struct (c, fields, Dim) The cell array is converted to a struct array, note that fields is a char array or a cell array, and size (c,dim) = = Length (fields ) % If fields is a cell arraysize (c,dim) = = Size (fields,1)% if fields are a char array This often error is the fields Type and Dim not
    • C = Struct2cell (s) struct array converted to cell array this is basically nothing wrong.


Matrix and Cell Arrayc = Mat2cell (x, M, N) converted to m row N column cell arraym = Cell2mat (c) must be of the same type, and the restriction cannot contain the cell array or the object type , but the struct structure is possible (again this struct does not contain cell and type of object, otherwise error is still
double and Cell Array c = Num2cell (A, [Dim1, DIM2, ...]) Returns the dimension of C as Numel (A)/prod (x, y,...)     dimn is an integer, ranging from 1 to Ndims (a) Only a numeric matrix can be converted directly to a cell, and there is no way for the cell to be converted to double. If you can use   cell2mat Span class= "Apple-converted-space" >  or Cat (dim,c{:}). Many times it's convenient Cell Arrays of Strings Listed separately is because many times have to touch this, the basic TXT and the like read the value data is a char cell array ~ You can use the cell parameter with the character-related part of the function ( Basic support)

    • cellstr       convert a character array to a cell array of strings. The trailing blanks are removed span>
    • char         convert a cell array of strings to a character array. Restores the blank space lost when converting
    • deblank   remove trailing Blanks from a string.
    • iscellstr     Return true for Acell array of strings.
    • sort          sort.
    • The
    • strcat connection character.
    • The
    • strcmp comparison character.
    • Strmatch find characters.
    • Strrep Substitution characters
    • The
    • RegExp series and Accumarray support row and column vectors.

Reading and writing data read in, after processing of course is required to save, but the face of the requirements you always very helpless, if you can. Mat format that's great, but most of the requirements are txt. (Do not understand why the database, such as MATLAB is also supported ... Alas, demand is always the last footnote ...) First

The first thing to know is how the cell array is generated: see Textscan for details. Large file read first recommend this function, processing flexibility can save a lot of effort, the specific format is key! Can effectively separate the results of cell data to facilitate processing ~ Otherwise 30 million data loops are definitely out of memory ... Try to use advanced IO to read and write ... In addition, 7.0 many reads are numeric values that return the cell's char type array 7.6 or more using double, including xlsread ... if allowed, Xlswrite is the best choice ~ The result of a lot of data testing is still very good. examples of loops in MATLAB help:

    1. MyCell = {' A ' 1 2 3; ' B ' 4 5 6};
    2. [nrows,ncols]= size (MyCell);
    3. filename = ' Celldata.dat ';
    4. FID = fopen (filename, ' w ');
    5. For Row=1:nrows
    6. fprintf (FID, '%s%d%d%d\n ', Mycell{row,:});
    7. End
    8. Fclose (FID);
Copy Code

Only values can be considered first Cell2mat and then csvwrite.
Cellfun example can refer to MATLAB company's Http://www.mathworks.cn/support/solutions/en/data/1-1190ZB/index.html?solution=1-1190ZB

Summarize
The basic is to summarize the usage, especially the conversion and preservation aspects, relative to the struct array, because of the support of MATLAB, it is often used. Weak function, but basically according to the rules, still can minimize errors.
I hope you will exchange the experience of use ~

Introduction to MATLAB cell arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.