Link to home
Start Free TrialLog in
Avatar of libertyforall2
libertyforall2Flag for United States of America

asked on

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

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

Avatar of yuk99
yuk99
Flag of United States of America image

To detect file size use DIR command:

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

Open in new window

Avatar of libertyforall2

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great! Thanks.