Clear Filters
Clear Filters

Sorting Cell stucture with different data types

1 view (last 30 days)
I have a cell structure with 32 columns of data I read in the data in a cell Structure C = textscan(fid,'%s%f%s%f%s%f%f%f%f%f%f%f%f%f%f%f%s%s%f%f%f%f%f%f%f%f%s%f%f%s%f%s','delimiter',',');
I would like to sort the records by col 12 and keep all rows elements together, like sortrow
I tried this but it doesn't work [d,ix]=sort([C{:,1}]); cs=C(ix,:); I get his error ??? Index exceeds matrix dimensions.
===Begin Data == 3-Nov-00 9483 20-Jan-01 45 C 7.5 7.875 7.6875 1651 0 0.763201 0.649645 0.022872 -0.04417 0.081764 0.044075 A AGILENT TECHNOLOGIES INC. 20001103 9483 44.87318 44.93208 43.2243 44.10763 46.8125 0 N/A 0 0 N/A 0 N/A 3-Nov-00 9483 20-Jan-01 40 C 10.125 10.75 10.4375 1458 0 0.764967 0.763405 0.019358 -0.038622 0.069405 0.049576 A AGILENT TECHNOLOGIES INC. 20001103 9483 44.87318 44.93208 43.2243 44.10763 46.8125 0 N/A 0 0 N/A 0 N/A 3-Nov-00 9483 20-Jan-01 35 C 13.625 14.25 13.9375 98 0 0.786377 0.859066 0.01393 -0.030619 0.051442 0.051967 A AGILENT TECHNOLOGIES INC. 20001103 9483 44.87318 44.93208 43.2243 44.10763 46.8125 0 N/A 0 0 N/A 0 N/A 3-Nov-00 9483 20-Jan-01 30 C 17.625 18.25 17.9375 308 0 0.797738 0.931167 0.008332 -0.020541 0.031331 0.051155 A AGILENT TECHNOLOGIES INC. 20001103 9483 44.87318 44.93208 43.2243 44.10763 46.8125 0 N/A 0 0 N/A 0 N/A ==End Data ===

Accepted Answer

Nathan Greco
Nathan Greco on 1 Jul 2011
How about this one:
[d ix] = sort(C{12});
newC = cellfun(@(x)x(ix),C,'un',0);
  3 Comments
Nathan Greco
Nathan Greco on 1 Jul 2011
I take it that you understand what sort does, so I'll skip over that.
cellfun is a useful function that saves you the time of writing loops.
The same thing could be written as:
for idx = length(C)
C{idx} = C{idx}(ix)
end
Breaking up cellfun:
@(x)x(ix) is an anonymous function. (The @ symbol clarifies this.) The (x) is the input to the function, so it is the variable that you will use to manipulate whatever you want.
x(ix) rearranges the input (x) according to ix (which we found in sorting).
The second input to cellfun is what is being "looped" over, so we put C.
The third/fourth input clarifies that the output is not going to be uniform. ("UniformOutput",0) The zero (0) signifies false. (This forces the result to be placed in a cell array, which is what you wanted anyways.)
luke
luke on 5 Jul 2011
Working with this code, I tried to change the sort fields to 1,3,4
like this
[d ix] = sort(C{1,3,4});
newC = cellfun(@(x)x(ix),C,'un',0);
and I get this error message
??? Index exceeds matrix dimensions.

Sign in to comment.

More Answers (2)

luke
luke on 1 Jul 2011
Also, all of the string data are cell arrays. So this is a Cell Array in a Cell Array

luke
luke on 1 Jul 2011
This is what C looks like
cell <3503x1 double> <3503x1 cell> <3503x1 double> <3503x1 cell> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 cell> <3503x1 cell> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 double> <3503x1 cell> <3503x1 double> <3503x1 double> <3503x1 cell> <3503x1 double> <3503x1 cell>

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!