How to rewrite this Matlab 2014 code with axis-subplots to 2016?

假装没事ソ 提交于 2019-12-08 15:38:21

问题


Code which I wrote for Matlab 2014 but which I want to rewrite it to Matlab 2016 such that it becomes more compact, since it is extranous now

hFig3=figure('Units', 'inches');
hax3_b1=axes('Parent', hFig3);
hax3_b2=axes('Parent', hFig3);
hax3_b3=axes('Parent', hFig3);
hax3_b4=axes('Parent', hFig3);

b1=subplot(2,2,1, hax3_b1); 
b2=subplot(2,2,2, hax3_b2); 
b3=subplot(2,2,3, hax3_b3); 
b4=subplot(2,2,4, hax3_b4);

% Example of common expression
set([b1 b2], 'Units', 'inches'); % http://stackoverflow.com/a/39817473/54964

u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b1, u, y, C); 
hold on; 
plot(b2, u'); 
histogram(b3, u');
histogram(b4, u);
hold off; 
drawnow; 

Output is ok

OS: Debian 8.5 64 bit
MATLAB: 2014 but want to convert to 2016


回答1:


You can simply write:

figure('Units','inches');
b1 = subplot(2,2,1); 
b2 = subplot(2,2,2); 
b3 = subplot(2,2,3); 
b4 = subplot(2,2,4);

or preferably, if you want to have an array of the axes:

figure('Units','inches');
b(1) = subplot(2,2,1);
b(2) = subplot(2,2,2);
b(3) = subplot(2,2,3);
b(4) = subplot(2,2,4);

or use a simple for loop:

figure('Units','inches');
b(1:4) = axes;
for k = 1:numel(b)
    b(k) = subplot(2,2,k);
end

In any option you choose there is no need for all the axes commands.

Here is all your 'demo' code:

b(1:4) = axes;
for k = 1:numel(b)
    b(k) = subplot(2,2,k);
end
set(b(1:2), 'Units', 'inches');
u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b(1), u, y, C); 
plot(b(2), u'); 
histogram(b(3), u');
histogram(b(4), u);

There might be no real need also in the figure command, it depends on what do with it.




回答2:


If you're just looking for shorter you can eliminate most of the stuff at the beginning. You don't need the figure but I keep it as a matter of habit.

fh = figure;
x = 1:4;
b = arrayfun(@(y) subplot(2,2,y), x, 'UniformOutput',0);
b{1}.Units = 'inches';
b{2}.Units = 'inches';
u=0:0.1:1; y=sin(u); C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(b{1}, u, y, C); 
plot(b{2}, u'); 
histogram(b{3}, u');
histogram(b{4}, u);


来源:https://stackoverflow.com/questions/39875016/how-to-rewrite-this-matlab-2014-code-with-axis-subplots-to-2016

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!