How do I print a legend using a character array and integer array?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shelley Snider
le 18 Jan 2023
Commenté : Fangjun Jiang
le 19 Jan 2023
I want to print a legend that looks like this:
1st entry: 'Current solution, 4800 m DR'
2nd entry: 'Current solution, 7000 m DR'
3rd entry: 'Current solution, 9000 m DR'
4th entry: 'Archive solution, 4800 m DR'
5th entry: 'Archive solution, 7000 m DR'
6th entry: 'Archive solution, 9000 m DR'
The numbers come from a structure array. I'm trying to use sprintf, but I'm having troubles with the format of the 'Current' and 'Archive' strings. A character array does not work since sprintf iterates through each character instead of each word. Do I need to do a cell array and then do a comma separated list for that?
1 commentaire
Walter Roberson
le 19 Jan 2023
A character array does not work since sprintf iterates through each character instead of each word.
A = 'Current'
sprintf('->%s<-', A)
sprintf('->%c<-', A)
You were using the wrong format item.
Réponse acceptée
Stephen23
le 19 Jan 2023
COMPOSE() is perfect for this, but was first introduced in R2016b. You could use ARRAYFUN() or CELLFUN():
C = {'Current','Current','Current','Archive','Archive','Archive'};
V = [4800,7000,9000,4800,7000,9000];
fnh = @(t,n)sprintf('%s solution, %d m DR',t,n);
lgd = cellfun(fnh,C(:),num2cell(V(:)), 'uni',0)
0 commentaires
Plus de réponses (1)
Fangjun Jiang
le 18 Jan 2023
Modifié(e) : Fangjun Jiang
le 18 Jan 2023
a="current";
b={'archive'};
c='current';
sprintf('%s',a)
sprintf('%s',b{1})
sprintf('%s',c)
2 commentaires
Fangjun Jiang
le 19 Jan 2023
Another trick is below, if you put the data in the right format
data={'1st','2nd','3rd';'Current','Archive','Current';4800,7000,9000};
sprintf('%s entry: ''%s solution, %d m DR''\n',data{:})
Voir également
Catégories
En savoir plus sur Cell Arrays 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!