Can MATLAB create a column of words based on integer values in another column?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I’m parsing out columns of data from several text files. One of the columns of data pertains to sub-assembly IDs with a possible range of values between 2 and 103. Depending on the size of the text files, many of these values could easily be repeated numerous times. It’s also worth noting that not all of the known sub-assembly IDs are present in each text file, and they are not always in numerical order. While the nomenclature of each sub-assembly ID is known, they were never included in the text files.
The known sub-assembly ID values are 2, 3, 6, 8, 9, 10, 14, 22, 23, 24, 39, 42, 43, 44, 47, 48, 100, 101, 102, and 103.
A sample of one of these this would be: Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8];
Is there a way I can tell MATLAB to automatically create a column of words (the nomenclature in this case) for each and every one of the Sub_ID values?
If successful, the result would look like this:
2 Sub_ID2
3 Sub_ID3
3 Sub_ID3
9 Sub_ID9
2 Sub_ID2
6 Sub_ID6
23 Sub_ID23
23 Sub_ID23
23 Sub_ID23
42 Sub_ID42
100 Sub_ID100
8 Sub_ID8
42 Sub_ID42
43 Sub_ID43
43 Sub_ID43
8 Sub_ID8
Any ideas on how to approach this are greatly appreciated.
Thank you.
0 commentaires
Réponse acceptée
Azzi Abdelmalek
le 10 Oct 2013
Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8];
for k=1:numel(Sub_ID)
out{k,1}=sprintf('Sub_ID%d',Sub_ID(k))
end
2 commentaires
Jan
le 11 Oct 2013
@Brad: Of course a pre-allocation is essential as usual. So do not forget to initialize the array out by:
out = cell(numel(Sub_ID), 1);
Plus de réponses (2)
Jos (10584)
le 10 Oct 2013
Take a look at sprintf and arrayfun :
IDvalues = [1 3 100 12]
IDnames = arrayfun(@(x) sprintf('Sub_ID%d',x), IDvalues, 'un',0)
IDnames is a cell array.
0 commentaires
Jan
le 11 Oct 2013
Modifié(e) : Jan
le 11 Oct 2013
And a third idea:
Str = sprintf('Sub_ID%d*', Sub_ID);
out = regexp(Str(1:end-1), '*', 'split');
But "the result would look like this" could mean something completely different, perhaps:
Sub_ID = [2; 3; 3; 9; 2; 6; 23; 23; 23; 42; 100; 8; 42; 43; 43; 8] .';
fprintf('% Sub_ID%d\n', cat(1, Sub_ID, Sub_ID))
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!