フィルターのクリア

order using first column in array cell

42 ビュー (過去 30 日間)
Luca Re
Luca Re 2024 年 7 月 20 日 20:25
編集済み: Stephen23 2024 年 7 月 21 日 4:27
newList=sortrows(newList,1);
Error using matlab.internal.math.cellstrpad
Cell elements must be character arrays.
Error in sortrows>sortBackToFrontCell (line 137)
tmp = matlab.internal.math.cellstrpad(A(I,ack));
Error in sortrows (line 77)
I = sortBackToFrontCell(A, col);

採用された回答

Stephen23
Stephen23 2024 年 7 月 21 日 4:13
編集済み: Stephen23 2024 年 7 月 21 日 4:27
The basic problem is that you are storing scalar strings in a cell array. This should be avoided: "If you create variables that have the string data type, store them in string arrays, not cell arrays"
"Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages that come from using string arrays. And in fact, most functions do not accept cell arrays of strings as input arguments, options, or values of name-value pairs."
Lets simply convert those strings into character vectors using the (for historical-reasons rather badly named) CELLSTR function (which actually converts into a cell array of character vectors):
C = load('matlab_cell.mat').newList;
C(:,1) = cellstr(C(:,1));
C = sortrows(C,1)
C = 8x15 cell array
Columns 1 through 13 {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} Columns 14 through 15 {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2024 年 7 月 20 日 20:28
sort_order = sort(newList(:,1));
newList = newList(sort_order,:);
  1 件のコメント
Luca Re
Luca Re 2024 年 7 月 20 日 20:35
K>> sort_order = sort(newList(:,1));
Error using sort
Input argument must be a cell array of character vectors.

サインインしてコメントする。


Image Analyst
Image Analyst 2024 年 7 月 21 日 0:30
Fixed code below. Read comments for explanation.
s = load('matlab_cell.mat');
originalList = s.newList
originalList = 8x15 cell array
Columns 1 through 13 {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {["ES_Live_MPV ..."]} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {["ES_Live_MPV ..."]} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {["ES_Live_MPV ..."]} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {["ES_Live_MPV ..."]} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {["ES_Live_MPV ..."]} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} Columns 14 through 15 {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}
% The problem with sorting is that some cells in column 1 contain strings
% while other cells contain character arrays -- they different!
% Convert strings in column 1 into character arrays so that sortrows will work.
for row = 1 : height(originalList)
% Copy the other columns as is.
newList(row, :) = originalList(row, :);
thisCellContents = originalList{row, 1};
if isstring(thisCellContents)
% If it's a string (double quotes) turn it into a character array (single quotes).
thisCellContents = char(thisCellContents);
newList{row, 1} = thisCellContents;
end
end
% Sort the new list, not the original list.
sortedCellArray = sortrows(newList, 1)
sortedCellArray = 8x15 cell array
Columns 1 through 13 {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2024/05/01'} {'From instrument'} {[0]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2008/01/01'} {'From instrument'} {[6]} {'Trend'} {'Intraday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} {'ES_Live_MPV Es...'} {[1]} {'ES'} {'On Micro'} {'2023/12/01'} {'From instrument'} {[0]} {'Trend'} {'Multiday'} {'No'} {[1]} {[0]} {[0]} Columns 14 through 15 {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by