Matrix with one numbers column and one strings column.

2 vues (au cours des 30 derniers jours)
Graur Alin
Graur Alin le 14 Juil 2015
Commenté : Walter Roberson le 15 Juil 2015
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.

Réponses (1)

Jos (10584)
Jos (10584) le 14 Juil 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 commentaires
Graur Alin
Graur Alin le 14 Juil 2015
Can I sort a cell array then? I have to display the results in a descending order.
Walter Roberson
Walter Roberson le 15 Juil 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,:);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by