Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-22
4
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

715 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