Multiple plots coombine and concatenate in matlab [closed]

老子叫甜甜 提交于 2019-11-30 10:10:20

问题


The problem is that I have a for loop as follows:

for i=1:(rx * tx)    
    for j=1:(rx * tx)
        eval(sprintf('t_%d_%d_t = corrcoef(m_a%d_abs, m_b%d_abs)', pairs(i), pairs(j), pairs(i), pairs(j)));
        eval(sprintf('t_%d_%d = t_%d_%d_t(1, 2)', pairs(i), pairs(j), pairs(i), pairs(j)));

        if(eval(sprintf('t_%d_%d', pairs(i), pairs(j))) >= max_cor)
            eval(sprintf('figure(''name'', ''a (%d) <> b (%d)'')', pairs(i), pairs(j)));
            eval(sprintf('plot(a%d, ''r'')', pairs(i)));
            hold on;
            eval(sprintf('plot(b%d, ''b'')', pairs(j)));
            xlabel('Tones (f)');
        end
    end
end

this for loop plots each Plot in a new window according to the if statement like this plot

I want to concatenate each next plot after the last plot so that all plots are combined on one plot but concatenated. I hope, you understand me. Many thanks.


回答1:


Your code should be taught as an example for How you should never write code in Matlab!, your massive use of eval make it virtually impossible to follow your code. Hence, I'll just drop here what I think may solve your problem, but for any explanations, you would first need to rewrite your code in a proper way (you may want to read this and this).

c = 0;
for k=1:(rx * tx)    
    for m=1:(rx * tx)
        eval(sprintf('t_%d_%d_t = corrcoef(m_a%d_abs, m_b%d_abs)', pairs(k), pairs(m), pairs(k), pairs(m)));
        eval(sprintf('t_%d_%d = t_%d_%d_t(1, 2)', pairs(k), pairs(m), pairs(k), pairs(m)));
        if(eval(sprintf('t_%d_%d', pairs(k), pairs(m))) >= max_cor)
            nx = eval(sprintf('numel(a%d)', pairs(k)));
            X = c+1:(c+nx);
            eval(sprintf('plot(X,a%d, ''r'')', pairs(k)));
            hold on;
            eval(sprintf('plot(X,b%d, ''b'')', pairs(m)));
            c = c+nx;
            xlabel('Tones (f)');
        end
    end
end



回答2:


Move the figure command out of the loop, only opening one figure for all plots:

figure;

for i=1:(rx * tx)    
    for j=1:(rx * tx)
        eval(sprintf('t_%d_%d_t = corrcoef(m_a%d_abs, m_b%d_abs)', pairs(i), pairs(j), pairs(i), pairs(j)));
        eval(sprintf('t_%d_%d = t_%d_%d_t(1, 2)', pairs(i), pairs(j), pairs(i), pairs(j)));

        if(eval(sprintf('t_%d_%d', pairs(i), pairs(j))) >= max_cor)
            eval(sprintf('(''name'', ''a (%d) <> b (%d)'')', pairs(i), pairs(j)));
            eval(sprintf('plot(a%d, ''r'')', pairs(i)));
            hold on;
            eval(sprintf('plot(b%d, ''b'')', pairs(j)));
            xlabel('Tones (f)');
        end
    end
end


来源:https://stackoverflow.com/questions/44737783/multiple-plots-coombine-and-concatenate-in-matlab

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