How do I get MATLAB to automatically build a column of cell array data based on an array of varying numerical values?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I've been asked to build an M-file to help automate some plotting activities which utilize the gscatter command. Specifically, use the functionality of gscatter, and apply specific verbiage for the legend based on varying values. I've read about numerous attempts to automate plot creation - a lot of which is practical and useful. But when it comes to automating plot creation using the gscatter command, info is a little scarce.
So I started off by using the MATLAB example for the gscatter function. I typed the following into MATLAB:
load carsmall;
gscatter(Weight,MPG,Model_Year,'','xos');
I got the same plot as shown here:
Next, I created a cell array column (equal in size to the Model_Year data) containg the actual model years (in words) and called it LegCol: LegCol = {'seventy';'seventy'; .....'seventy six';'seventy six';.....'eighty two';'eighty two'};
Finally, I re-issued the gscatter function:
gscatter(Weight,MPG,LegCol,'','xos');
I get the same plot as before and the legend now contains the model years (in words) - as expected.
The approach seems to work, but I need a way for MATLAB to automatically create the cell array LegCol (based on the model years) - instead of doing it by hand.
Is there a way to do this?
0 Kommentare
Akzeptierte Antwort
A Jenkins
am 23 Okt. 2013
Bearbeitet: A Jenkins
am 23 Okt. 2013
There are lots of ways. A couple simple examples you can play with to help you get started:
1)
year_names={70, 'seventy';
76, 'seventy six';
82, 'eighty two'};
for idx=1:size(Model_Year,1)
for jdx=1:size(year_names,1)
if Model_Year(idx)==year_names{jdx,1}
LegCol{idx}=year_names{jdx,2};
end
end
end
2)
for idx=1:size(Model_Year,1)
switch Model_Year(idx)
case 70
LegCol{idx}='seventy';
case 76
LegCol{idx}='seventy six';
case 82
LegCol{idx}='eighty two';
end
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Bar Plots finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!