plotting within a for loop

2 views (last 30 days)
Christopher
Christopher on 17 Feb 2013
I have a for loop that takes 2 matrix 365x24 and plots each corresponding matrix line ,i.e. X(3,1:24) and Y(3,1:24), and plots it. I would like to be able to have a figure for every iteration so i can come back to it a look for it. In the long run i should have 365 accessible figures in the workspace. And if i can then take these 365 figures and store then in a folder

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 17 Feb 2013
Edited: Azzi Abdelmalek on 20 Feb 2013
for k=1:365
h=plot(X(k,:),Y(k,:))
hgsave(h,'sprintf('fig%d',k))
close
end
To load your plots use
hgload('figurename')
  5 Comments
Christopher
Christopher on 20 Feb 2013
%Zone C Data for 2011
clear all
%Data represents JAN 2 - DEC 31 DATE_E = xlsread('Zone_C.xlsx',1,'C25:C8760'); % DATE FROM EXCEL
DA_LBMP_E = xlsread('Zone_C.xlsx',1,'I25:I8760'); %DA_LMBP DATA FROM EXCEL
DA_REG_E = xlsread('Zone_C.xlsx',1,'K25:K8760'); %DA_REGULATION DATA(DOLLARS)FROM EXCEL
DA_CON_E = xlsread('Zone_C.xlsx',1,'P25:P8760'); %DA_CONGESTION DATA FROM EXCEL
TEMP_E = xlsread('Zone_C.xlsx',1,'R25:R8760'); %TEMPERATURE DATA (F)FROM EXCEL
HUMIDITY_E = xlsread('Zone_C.xlsx',1,'S25:S8760'); %HUMIDITY DATA FROM EXCEL
WIND_E = xlsread('Zone_C.xlsx',1,'T25:T8760'); %WIND SPEED DATA (MPH?)FROM EXCEL
LOAD_E = xlsread('Zone_C.xlsx',1,'U25:U8760'); %LOAD DATA (MW) FROM EXCEL
GAS_E = xlsread('Zone_C.xlsx',1,'V25:V8760'); %GAS PRICE DATA (DOLLARS) FROM EXCEL
%NOW REPRESENT DATA IN MATRIX FORM
HOUR = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23];% VECTOR SO WE CAN PLOT ACCORDING TO THE HOUR OF THE DAY
DATE = reshape(DATE_E,24,[]);
DA_LBMP = reshape(DA_LBMP_E,24,[]); %RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
DA_REG = reshape(DA_REG_E,24,[]); %RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
DA_CON = reshape(DA_CON_E,24,[]); %RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
TEMP = reshape(TEMP_E,24,[]);%RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
HUMIDITY = reshape(HUMIDITY_E,24,[]);%RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
WIND= reshape(WIND_E,24,[]);%RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
LOAD = reshape(LOAD_E,24,[]);%RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
GAS = reshape(GAS_E,24,[]);%RESHAPE COLUMN DATA INTO 24 X 365 MATRIX
%NOW THAT WE HAVE MATRIX OF OUR DATA, WE CAN PLOT ACCORDING TO THE HOURS OF
%THE DAY FOR EACH VARIBLE
%FOR LOOP TO SIMULTANEOUSLY GRAPH ALL VARIABLE
%Figure 1
for k=1:364
hold on
subplot(2,3,1)
plot(HOUR,DA_LBMP(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');title('DA_LBMP');
hold on
subplot(2,3,2)
plot(HOUR,DA_REG(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');ylabel('Price($)'); title('DA_REGULATION');
hold on
subplot(2,3,3)
plot(HOUR,DA_CON(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');title('DA_congestion');
hold on
subplot(2,3,4)
plot(HOUR,LOAD(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');ylabel('LOAD(MW)'); title('Load')
hold on
subplot(2,3,5)
plot(HOUR,GAS(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');ylabel('Price($)'); title('Gas prices')
hold on
subplot(2,3,6)
plot(HOUR,TEMP(:,k)); %PLOTS HOUR V DA_LBMP ON A SPECIFIC DAY
xlabel('hour');ylabel('Temp(F)'); title('Temperature')
end
%AZZI THIS IS WHERE I AM HAVING TROUBLE. AS SOON AS I RUN THIS THE FIGURES
%GET STORED INTO THE MATLAB FOLDER BUT THEY DONT HAVE THE GRAPHICAL
%INFORMATION I NEED
for k=1:364
plot(HOUR,LOAD(:,k));
hgsave(sprintf('plot%d.jpg',k));
close
end
Christopher
Christopher on 20 Feb 2013
hey azzi i got it to work ! thanks for your help! you are the man!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 Feb 2013
You might want to save them as PNG files so that you can see them in the operating system as thumbnails, that is, if you don't need to interact with them via the figure toolbar anymore.
yourFolder = pwd; % Or whatever folder you want to store them in.
for k=1:365
cla;
plot(X(k,:),Y(k,:));
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
baseFileName = sprintf('Figure #%d.png', k);
fullFileName = fullfile(yourFolder, baseFileName);
export_fig(fullFileName);
end
  3 Comments
Walter Roberson
Walter Roberson on 21 Feb 2013
Image Analyst did say you needed to download export_fig, and even gave you the URL.
Christopher
Christopher on 21 Feb 2013
i downloaded export fig. in what folder should i put it in so matlab can find the code ?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!