sort arrays with sortrows
2 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Jarrod Rivituso
on 1 May 2011
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)
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!