Combine string elements and number elements in one matrix or cell array.

15 vues (au cours des 30 derniers jours)
Kiril Milev
Kiril Milev le 21 Août 2018
Réponse apportée : Adam Danz le 21 Août 2018
Hello guys,
How can I combine string and number elements in one matrix or cell array and how can I manipulate the data after that? Thanks :)
Kiril
  2 commentaires
Rik
Rik le 21 Août 2018
Have a read here (or here for more general advice) and here. It will greatly improve your chances of getting an answer.

Connectez-vous pour commenter.

Réponses (1)

Adam Danz
Adam Danz le 21 Août 2018
As Rik pointed out, your request is vague, but maybe this will get you started.
You must use a cell array to combine numbers and strings in the same variable.
Here's an example
bg = { 3 5 9; 'Sofia', 'Plovdiv', 'Burgas'}'
bg =
3×2 cell array
{[3]} {'Sofia' }
{[5]} {'Plovdiv'}
{[9]} {'Burgas' }
There's lots of ways to interact with this cell array (ie, 'manipulate the data').
Take the mean of column 1
mean([bg{:}])
Find the value in column 1 associated with whatever row contains 'Burgas' in column 2
bg{strcmp(bg(:,2), 'Burgas'), 1}
Get the data in the 2nd row, 1st column
bg(2,1) %the result is another cell (1 element)
bg{2,1} %the result is a matrix (1 element)
If there's something more specific you'd like to know, describe it in the comment section.

Catégories

En savoir plus sur Logical 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