Download code
From LiteratePrograms
Back to Plots_in_Matlab
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
example_of_plot_01.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/Plots_in_Matlab?action=history&offset=20071002061625 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/Plots_in_Matlab?oldid=10997 25 26 27 f=@(x)((x+1)./(x.*sin(x)+1)) 28 29 30 x_bounds = [-3, 3]; 31 x_resolution = .1; 32 x_domain = (x_bounds(1):x_resolution:x_bounds(2))' 33 34 35 y = f(x_domain); 36 37 38 figure; 39 plot(x_domain,y) 40 41 42
example_of_plot_with_legend_01.m
1 % Copyright (c) 2008 the authors listed at the following URL, and/or 2 % the authors of referenced articles or incorporated external code: 3 % http://en.literateprograms.org/Plots_in_Matlab?action=history&offset=20071002061625 4 % 5 % Permission is hereby granted, free of charge, to any person obtaining 6 % a copy of this software and associated documentation files (the 7 % "Software"), to deal in the Software without restriction, including 8 % without limitation the rights to use, copy, modify, merge, publish, 9 % distribute, sublicense, and/or sell copies of the Software, and to 10 % permit persons to whom the Software is furnished to do so, subject to 11 % the following conditions: 12 % 13 % The above copyright notice and this permission notice shall be 14 % included in all copies or substantial portions of the Software. 15 % 16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 % 24 % Retrieved from: http://en.literateprograms.org/Plots_in_Matlab?oldid=10997 25 26 27 data1 = struct('date', (1:100)', 'value', 100*exp(cumsum(randn(100,2))/10), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 1') 28 data2 = struct('date', (1:100)', 'value', 100*cumsum(rand(100,2)*2-1+.1), 'names', {{ 'line 1', 'line 2'}}, 'title', 'dataset 2') 29 30 %% plot 31 % notice the handle I get back from the plots 32 figure; 33 h1 = plot(data1.date, data1.value, 'linewidth',2) 34 hold on 35 h2 = plot(data2.date, data2.value, ':', 'linewidth',2) 36 hold off 37 %% Legend 38 legend(gca,[h1;h2], cellfun(@(x,y)([x ' ' y]), ... 39 {data1.names{:}, data2.names{:}}', ... 40 cellstr( [repmat(data1.title, length(data1.names),1); ... 41 repmat(data2.title,length(data2.names),1)]), ... 42 'uniformoutput',false), 'Location', 'NorthWest' ) 43
