Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://www.mathworks.com/help/matlab/ref/sortrows.html" rel="nofollow"><code>sortrows</code></a> function is designed to handle this and works for cell arrays:</p> <blockquote> <p><code>sortrows(X,COL)</code> sorts the matrix based on the columns specified in the vector <code>COL</code>.</p> </blockquote> <p>Say you have your datatable <code>T</code> formatted as a cell. For example, this random data:</p> <pre><code>&gt;&gt; T = [mat2cell(char(randi(26,4,3)+64),ones(4,1),ones(3,1)) num2cell(rand(4,3))] T = 'W' 'M' 'D' [0.5795] [0.1008] [0.1439] 'Z' 'X' 'S' [0.1030] [0.5029] [0.3192] 'R' 'I' 'U' [0.3553] [0.4873] [0.4491] 'L' 'I' 'U' [0.9389] [0.7817] [0.0499] </code></pre> <p>To sort according to column #4 in ascending order:</p> <pre><code>&gt;&gt; TSortedCol4 = sortrows(T,4) TSortedCol4 = 'Z' 'X' 'S' [0.1030] [0.5029] [0.3192] 'R' 'I' 'U' [0.3553] [0.4873] [0.4491] 'W' 'M' 'D' [0.5795] [0.1008] [0.1439] 'L' 'I' 'U' [0.9389] [0.7817] [0.0499] </code></pre> <hr> <p>If you need to get your data into the format of <code>T</code> above from a text file, you can do the following to read and reformat it:</p> <pre><code>fid = fopen('testtable.txt','r'); Tin = textscan(fid, '%q %q %q %f %f %f'); % each column has a cell strcols=cellfun(@iscell,Tin); T = [Tin{strcols} num2cell([Tin{~strcols}])]; % each item has a cell fclose(fid); </code></pre> <p>As in your example, the columns are delimited by white space. The strings can include spaces if double quotes are used in the text file because of the <code>%q</code> format specifier.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload