Matrix with one numbers column and one strings column.

2 visualizaciones (últimos 30 días)
Graur Alin
Graur Alin el 14 de Jul. de 2015
Comentada: Walter Roberson el 15 de Jul. de 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.

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by