Sorting Cell stucture with different data types
2 次查看(过去 30 天)
显示 更早的评论
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 ===
0 个评论
采纳的回答
Nathan Greco
2011-7-1
How about this one:
[d ix] = sort(C{12});
newC = cellfun(@(x)x(ix),C,'un',0);
3 个评论
Nathan Greco
2011-7-1
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.)
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!