sort arrays with sortrows
2 次查看(过去 30 天)
显示 更早的评论
Hello,
%I have the below txt file.
Stock Date Time Price Volume Stock Category
ETE 04/01/2010 10145959 18.31 500 Big Cap
ETE 04/01/2010 10150000 18.01 70 Big Cap
ETE 04/01/2010 10170000 18.54 430 Big Cap
ABC 04/01/2010 10190000 18.34 200 Big Cap
YYY 04/01/2010 10200000 18.34 100 Big Cap
% How can I sort this rows with respect the first column (alphabetically or not) using sortrows ( or something else), so the file will look like:
ABC 04/01/2010 10190000 18.34 200 Big Cap
ETE 04/01/2010 10145959 18.31 500 Big Cap
ETE 04/01/2010 10150000 18.01 70 Big Cap
ETE 04/01/2010 10170000 18.54 430 Big Cap
YYY 04/01/2010 10200000 18.34 100 Big Cap
Any hint?
Many thanks in advance
Panos
1 个评论
采纳的回答
Jarrod Rivituso
2011-5-1
How are you reading the data into MATLAB? I think the format you have the data stored in could make or break sortrows in certain cases.
Here's a method where sortrows would work...
%Open file
fid = fopen('myFile.txt');
%Read data in as 1-by-7 cell array of each column
data = textscan(fid,'%s%s%s%s%s%s%s')
%Convert data to 5-by-7 cell array of each data element
data2 = [data{:}]
%Sort data based on first row. Characters can be treated as numeric, so this works
dataSorted = sortrows(data2,1)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!