for循环中相同图中的多个图

时间:2014-07-21 03:16:43

标签: matlab loops plot figure

我很抱歉,因为这似乎是一个刻板的常见问题,然而,即使在照顾'坚持,我也无法弄清楚我在做什么。我的阴谋被相互覆盖。我想在同一图中获得最内层循环的多个图。请建议。

userName={'A' 'B' 'C'  'Z' 'R'};
timeCategories={'All' 'Morning' 'Afternoon' 'Evening'};
for user=1:5
    currentUserName= char(userName(user));    
    for K=1:4
        method='Al';
        fig=figure();
        xlabel('Recall', 'FontSize',12,'FontWeight','bold');
        ylabel('Precision','FontSize',12, 'FontWeight','bold');
       titleOfFig=strcat('PrecisionRecall\_',currentUserName,'\_','Top',num2str(K));
        title(titleOfFig,'FontSize',17,'FontWeight','bold'); 
        set(gca,'XColor',[.0 .3 .2],'YColor',[.0 .3 .2],'LineWidth',2, 'FontSize',11, 'FontWeight','bold');
        axis([0 1 0 1]);
        grid on;
        filename = sprintf('%sPrecisionRecall_%s_Top%d.png',path,currentUserName,K); %path,'PrecisionRecall\_',userName(1),'\_','Top',num2str(K),'.png');
        hold on;
        for timeCat=1:4
            timeCategory=char(timeCategories(timeCat));
            precisionFileName=strcat(method,timeCategory,'top',num2str(K),...
            'Precision.csv');
            recallFileName=strcat(method,timeCategory,'top',num2str(K),'Recall.csv');
            Ptemp= csvread(precisionFileName);
            Rtemp= csvread(recallFileName);
            precisionVector=Ptemp(user,:);
            recallVector=Rtemp(user,:);
            h= plot(recallVector,precisionVector,'r--*','MarkerSize',8);
            hold on;
            set(h,'LineWidth',2);
        end
        legend(timeCategories,'Location','Best');
        print(fig, '-dpng',filename,'-r200');
     end 
end

userName={'A' 'B' 'C' 'Z' 'R'}; timeCategories={'All' 'Morning' 'Afternoon' 'Evening'}; for user=1:5 currentUserName= char(userName(user)); for K=1:4 method='Al'; fig=figure(); xlabel('Recall', 'FontSize',12,'FontWeight','bold'); ylabel('Precision','FontSize',12, 'FontWeight','bold'); titleOfFig=strcat('PrecisionRecall\_',currentUserName,'\_','Top',num2str(K)); title(titleOfFig,'FontSize',17,'FontWeight','bold'); set(gca,'XColor',[.0 .3 .2],'YColor',[.0 .3 .2],'LineWidth',2, 'FontSize',11, 'FontWeight','bold'); axis([0 1 0 1]); grid on; filename = sprintf('%sPrecisionRecall_%s_Top%d.png',path,currentUserName,K); %path,'PrecisionRecall\_',userName(1),'\_','Top',num2str(K),'.png'); hold on; for timeCat=1:4 timeCategory=char(timeCategories(timeCat)); precisionFileName=strcat(method,timeCategory,'top',num2str(K),... 'Precision.csv'); recallFileName=strcat(method,timeCategory,'top',num2str(K),'Recall.csv'); Ptemp= csvread(precisionFileName); Rtemp= csvread(recallFileName); precisionVector=Ptemp(user,:); recallVector=Rtemp(user,:); h= plot(recallVector,precisionVector,'r--*','MarkerSize',8); hold on; set(h,'LineWidth',2); end legend(timeCategories,'Location','Best'); print(fig, '-dpng',filename,'-r200'); end end

1 个答案:

答案 0 :(得分:0)

就像Naveen指出你没有使用你的循环索引;)请注意,你只需要为每个数字设置一次hold。因此,不需要在timeCat循环中保持。保持暂停状态,直到您打开一个新数字或召唤暂停时。

添加了通用数据,以表明以下代码正常运行。问题必须出在您的数据中,因为您的数据不依赖于timeCat。

@ironzionlion 是的,确实每个for循环都分配了变量P,但循环期间变量是相同的。因此我假设他忘了使用变量timeCat来识别正确的行。但是,您可能会调用错误的文件。他的其余代码工作正常,你可以在我的代码中看到使用通用数据。

请检查您从CSV文件中读取的数据,并了解如何访问此数据。

亲切的问候,

Ernst Jan

close all
clear all

userName={'A' 'B' 'C'  'Z' 'R'};
timeCategories={'All' 'Morning' 'Afternoon' 'Evening'};
for user=1:5
    currentUserName= char(userName(user));    
    for K=1:4
        method='Al';
        fig=figure();
        xlabel('Recall', 'FontSize',12,'FontWeight','bold');
        ylabel('Precision','FontSize',12, 'FontWeight','bold');
        titleOfFig=strcat('PrecisionRecall\_',currentUserName,'\_','Top',num2str(K));
        title(titleOfFig,'FontSize',17,'FontWeight','bold'); 
        set(gca,'XColor',[.0 .3 .2],'YColor',[.0 .3 .2],'LineWidth',2, 'FontSize',11, 'FontWeight','bold');
%         axis([0 1 0 1]);
        grid on;
        %filename = sprintf('%sPrecisionRecall_%s_Top%d.png',path,currentUserName,K); %path,'PrecisionRecall\_',userName(1),'\_','Top',num2str(K),'.png');
        hold on;
        for timeCat=1:4
            timeCategory=char(timeCategories(timeCat));
            precisionFileName=strcat(method,timeCategory,'top',num2str(K),...
            'Precision.csv');
            recallFileName=strcat(method,timeCategory,'top',num2str(K),'Recall.csv');
            Ptemp= timeCat.*[1:10];
            Rtemp= (1:10)/10;
            %precisionVector=Ptemp(1,:); % Call with loop variable!
            precisionVector=Ptemp;
            %recallVector=Rtemp(1,:); % Call with loop variable!
            recallVector=Rtemp;
            h= plot(recallVector,precisionVector,'r--*','MarkerSize',8);
            % hold on; You don't need this one I think.
            set(h,'LineWidth',2);
        end
        legend(timeCategories,'Location','Best');
        filename = [num2str(K),num2str(user)];
        print(fig, '-dpng',filename,'-r200');
     end 
end