Solved

Create conditional statement in existing matlab script below in case file is empty & keep existing images

Posted on 2011-02-22
4
363 Views
Last Modified: 2013-11-25
I need to  create conditional 'if' statements that are the main components of the scripts. I think I need to insert one more conditions into the statement that determines if the file is empty or not.  If it is empty it should direct MATLAB to create display screen output saying this and make sure that the 'saveas' statement for that location does NOT get executed...that way the .png file that is in the current MATLAB directory will remain unchanged.  I am not exactly clear on how to do this but my script is below

%so2.m - Matlab Script to plot so2 concentrations through time
%Input files:
    %so2location(1-10).txt ***excluding locations 8 and 9
    %text files with data for observation and forecast of so2.
    %NOTE locations 3,4,6 have only forecast data
%Output files:
    %so2location(1-10).png ***excluding locations 8 and 9
    %portable network graphics of time series for so2.
%---------------------------------------------------------------

%Initialize Counter
count=0;
for k=1:7 % loop through first seven locations
    count=count+1;
    %id the file and proceed accordingly
    if count==1
        fid = fopen('so2location1.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location1.png')
    elseif count==2
        fid = fopen('so2location2.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location2.png')    
    elseif count==3
    fid = fopen('so2location3.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        h=[p1];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location3.png')
    elseif count==4
        fid = fopen('so2location4.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        h=[p1];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location4.png')
    elseif count==5
        fid = fopen('so2location5.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location5.png')
    elseif count==6
        fid = fopen('so2location6.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1,p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location6.png')
    elseif count==7
        fid = fopen('so2location7.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location7.png')
    end
end


%Create Location 10 plot separate
        fid = fopen('so2location10.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=0.30*ones(1,length(datetime));
        good=0.2*ones(1,length(datetime));
        figure(10)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'yticklabel',[0.00 0.05 0.10 0.15 0.20 0.25 0.30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,0.3])
        title('SO2 Meteogram','fontsize',14,'fontweight','bold')
        ylabel('SO2 PPM')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so2location10.png')

Open in new window


%so4.m - Matlab Script to plot so4 concentrations through time
%Input files:
    %so4location(1-10).txt ***excluding locations 8 and 9
    %text files with data for observation and forecast of so4.
    %NOTE locations 3,4,6 have only forecast data
%Output files:
    %so4location(1-10).png ***excluding locations 8 and 9
    %portable network graphics of time series for so4.
%---------------------------------------------------------------

%Initialize Counter
count=0;
for k=1:7 % loop through first seven locations
    count=count+1;
    %id the file and proceed accordingly
    if count==1
        fid = fopen('so4location1.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location1.png')
    elseif count==2
        fid = fopen('so4location2.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location2.png')    
    elseif count==3
    fid = fopen('so4location3.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        h=[p1];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location3.png')
    elseif count==4
        fid = fopen('so4location4.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        h=[p1];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location4.png')
    elseif count==5
        fid = fopen('so4location5.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location5.png')
    elseif count==6
        fid = fopen('so4location6.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        h=[p1];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location6.png')
    elseif count==7
        fid = fopen('so4location7.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(k)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location7.png')
    end
end


%Create Location 10 plot separate
        fid = fopen('so4location10.txt');
        % read the file
        %Change fid when running as part of your script
        %Delete below and use the one above
        %To include fourth column of data
        %You will need a third '%f' in format argument below
        data = textscan(fid,'%s %s %f %f','delimiter',' ');
        fclose(fid);
        % get the date and time into a vector for use
        date=data{1};
        time=data{2};
        %adding a space to each element in the cell array
        %for correct format for input into datenum function
        pad={};
            for i=1:length(date)
                pad{i}=' ';
            end
        pad=pad';
        %Combine vector strings using the function 'strcat'
        datep=strcat(date,pad);
        datetime=strcat(datep,time);
        %**datenum converts to a 'serial' date that counts
        %units from initial time Jan 01 0000;
        %ingests each date and time and gives serial number
        datetime=datenum(datetime);
        % y axis data
        forecast=data{3};
        observation=data{4};
        % plot
        %'hold on' allows you to overlay multiple
        %data series on the same plot
        %for creating background colors use function 'area'
        moderate=30*ones(1,length(datetime));
        good=15.5*ones(1,length(datetime));
        figure(10)
        hold on
        area(datetime,moderate,'facecolor','y');
        area(datetime,good,'facecolor','g','edgecolor','k',...
        'linewidth',0.1);
        p1=plot(datetime,forecast,'ro-','linewidth',2);
        p2=plot(datetime,observation,'ko-','linewidth',2);
        h=[p1 p2];
        datetick('x','HH:MM mmm dd','keepticks')
        set(gca,'ytick',[0 5 10 15 20 25 30],...
        'yticklabel',[0 5 10 15 20 25 30],...
        'box','on','linewidth',2);
        %A buffer amount of 0.015 was added to y max
        %So that the data would not get blocked by
        %legend box itself
        xlim([min(datetime) max(datetime)])
        ylim([0,30])
        title('Sulfate Aerosols','fontsize',14,'fontweight','bold')
        ylabel('micrograms per cubic meter')
        legend(h,'forecast','observation','location','northwest')
        % set the right position and size
        %Not sure about this normalization of the axes on next line
        %set(gca,'Units','normalized','Position',[0.1 0.1 0.85 0.75]
        set(gcf,'units','inches')
        set(gcf,'position',[3 4.5 11 3.5],'paperposition',[3 4.5 11 3.5])
        set(gca,'units','inches')
        set(gca,'position', [0.75 0.75 9.5 2]);  
        %save current figure output to a .png file
        saveas(gcf,'so4location10.png')

Open in new window

0
Comment
Question by:libertyforall2
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:yuk99
ID: 34955506
To detect file size use DIR command:

f = dir('path/filename.txt');
if f.bytes > 0
    % do something
end

Open in new window

0
 

Author Comment

by:libertyforall2
ID: 34964328
SHould I place it before or after this below for each file? Since it already has one condition if count==1 for that file, how could I intergrate both?

%id the file and proceed accordingly
    if count==1
        fid = fopen('so4location1.txt');

Open in new window

0
 
LVL 15

Accepted Solution

by:
yuk99 earned 500 total points
ID: 34965782
Yes, in your current code you should do it for each file. You probably can avoid all those conditions with count. I didn't study your code close enough to make the conclusion. I believe your current code can be significantly simplified and made more maintainable. But this is a matter for another question.

If your input file is empty you probably want to avid any further processing and just go to the next one.

Try something like this.

%id the file and proceed accordingly
    if count==1
        f = dir('so4location1.txt');
        if f.bytes == 0
            continue
        end
        fid = fopen('so4location1.txt');
        % ...

Open in new window


CONTINUE statement stops the code (if file size is 0 bytes) and pass it to the next iteration in for-loop. Hope it will work.
0
 

Author Closing Comment

by:libertyforall2
ID: 34975190
Works great! Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question