Matrix with one numbers column and one strings column.
2 views (last 30 days)
Show older comments
Ok, so I have a for loop which returns me, for every file I read from a folder, the name of the file and some values I read from the file. I need to create a matrix with the first column containing the value from every specific file and the secound column should contain the name of the file from which that value was read. I need to sort those files descending by the values from each one of them.
Let's say I have three files: '1.txt', '2.txt', '3.txt'.
I read 1 from '1.txt', 5 from '2.txt' and 3 from '3.txt'. The final matrix should be like this [1 1.txt; 3 3.txt; 5 2.txt]
I hope I made myself clear.
0 Comments
Answers (1)
Jos (10584)
on 14 Jul 2015
You can use cell arrays for this, which can hold a mixture of types. Each cell contains something, possibly another cell array. To get a specific cell, use the standard notation with round brackets. To get the content of a cell, use curly brackets
C = {1 '1.txt' ; 2 '2.txt'}
OneCell = C(2,1)
Content = C{2,1}
2 Comments
Walter Roberson
on 15 Jul 2015
You can sort a column of a cell array and request the sort order as an output. You can then use the sort order to index the entire cell array.
[~, sortidx] = sort([A{:,1}]);
sortedA = A(sortidx,:);
See Also
Categories
Find more on Shifting and Sorting Matrices 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!