How do I move certain rows up or down in a cell array?

5 次查看(过去 30 天)
I have a cell array, which is sorted alphabetically. I need to move any row that contains a marker (for example, X), to the top of the list. How do I do that?
e.g.:
AAA AAB FFF FTD JJJ XXX XXY ZAF ZAE
So, I just need to move XXX and XXY to the beginning, and the rest of the array should remain the same.
Does anyone know of a script to do this automatically?
Thanks

采纳的回答

Matt J
Matt J 2012-10-23
编辑:Matt J 2012-10-23
idx=~cellfun('isempty', strfind(yourcell,'X'));
yourcell=yourcell(:).';
yourcell=[yourcell(idx), yourcell(~idx)];
  3 个评论
Matt J
Matt J 2012-10-23
Yes. If your cell array is really vertical and you want to keep it that way, just delete the 2nd line and concatenate vertically in the final line.
Ziggy Zigg
Ziggy Zigg 2012-10-23
Thanks Matt. I had another problem while using this. I had used a cell sorting function, for which I had to convert the cell array to char. So in Workspace, the cell contents show up with three apostrophes. This causes the strfind to not work (error: If any of the input arguments are cell arrays, the first must be a cell array of strings and the second must be a character array). If I do a 'iscellstr' to my array, it returns 1, meaning it is a cell array of strings. Do you know what those three apostrophes mean? I'm not familiar with that. Also, if I delete another column in that cell array, the apostrohes disappear, and now I can use the code you pasted, no problem. I'm stumped.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by