📄 fm_plot.m
字号:
function fm_plot(flag)
% FM_PLOT plot results of Continuation Power Flow,
% Optimal Power Flow and Time Domain
% Simulations.
%
% FM_PLOT(FLAG)
% FLAG 0 -> create variable list
% 1 -> plot selected variables
% 2 -> save graph
% 3 -> set layout
%
%Author: Federico Milano
%Date: 11-Nov-2002
%Update: 25-Feb-2003
%Update: 26-Jan-2005
%Version: 1.0.2
%
%E-mail: fmilano@thunderbox.uwaterloo.ca
%Web-site: http://thunderbox.uwaterloo.ca/~fmilano
%
% Copyright (C) 2002-2006 Federico Milano
global DAE Bus Syn Settings Fig Snapshot Hdl
global Varout Varname Path File OPF Theme GAMS
hdlfig = Hdl.axesplot;
hdlfig2 = Hdl.axeslogo;
Hdl_grid = findobj(Fig.plot,'Tag','Checkbox1');
Hdl_legend = findobj(Fig.plot,'Tag','Checkbox2');
Hdl_listvar = findobj(Fig.plot,'Tag','Listbox1');
Hdl_listplot = findobj(Fig.plot,'Tag','Listbox2');
Hdl_tipoplot = findobj(Fig.plot,'Tag','PopupMenu1');
Hdl_angref = findobj(Fig.plot,'Tag','PopupMenu2');
Hdl_snap = findobj(Fig.plot,'Tag','Radiobutton1');
hdl_zoom1 = findobj(Fig.plot,'Tag','Pushbutton12');
hdl_zoom2 = findobj(Fig.plot,'Tag','Pushbutton11');
hdl_zoom3 = findobj(Fig.plot,'Tag','Pushbutton4');
hdl_x = findobj(Fig.plot,'Tag','Pushbutton9');
hdl_y = findobj(Fig.plot,'Tag','Pushbutton5');
hdl_xy = findobj(Fig.plot,'Tag','Pushbutton10');
switch flag
case 'exporttext', % output data as plain text file
flag = 'plotvars';
out_matlab = 0;
out_mtv = 0;
out_text = 1;
case 'exportmtv', % output data as plain text file
flag = 'plotvars';
out_matlab = 0;
out_mtv = 1;
out_text = 0;
case 'exportscript', % output data as plain text file
flag = 'plotvars';
out_matlab = 1;
out_mtv = 0;
out_text = 0;
otherwise
out_matlab = 0;
out_mtv = 0;
out_text = 0;
end
switch flag
case 'initlist'
if ~strcmp(get(Fig.plot,'UserData'),File.modify)
set(Hdl_listvar,'String',enum(Varname.uvars(Varout.idx)),'Value',1);
Varname.changes = sparse(length(Varout.idx),1);
set(Fig.plot,'DefaultAxesColorOrder',Settings.color, ...
'DefaultAxesLineStyle','-');
end
set(Fig.plot,'UserData',File.modify)
case 'initxlabel'
first = strrep(Settings.xlabel,'\','');
hdl = findobj(Fig.plot,'Tag','Listbox1');
stringa = get(hdl,'String');
hdl = findobj(Fig.plot,'Tag','PopupMenu3');
if ~isempty(stringa)
set(hdl,'String',[{first}; stringa],'Enable','on','Value',1)
end
case 'plotvars'
if isempty(Varout.t)
fm_disp('Plotting Utilities: No data available for plotting.')
return
end
if isempty(Varname.po)
fm_disp('Plotting Utilities: Select variables to be plotted.')
return
end
nB = Bus.n;
nD = DAE.n;
Value = get(Hdl_listvar,'Value');
if isempty(Value), return, end
AxesFont = get(hdlfig,'FontName');
AxesColor = get(hdlfig,'Color');
AxesWeight = get(hdlfig,'FontWeight');
AxesAngle = get(hdlfig,'FontAngle');
AxesSize = get(hdlfig,'FontSize');
AxesUnits = get(hdlfig,'FontUnits');
plot_snap = get(Hdl_snap,'Value');
if plot_snap & ~OPF.init
snap_idx = [];
for i = 1:length(Snapshot);
a = find(Varout.t == Snapshot(i).time);
if isempty(a)
fm_disp(['Plotting utilities: Snapshots do not match ' ...
'current simulation data'],2)
Hdl_rad1 = findobj(gcf,'Tag','Radiobutton1');
set(Hdl_rad1,'Value',0);
plot_snap = 0;
snap_idx = [1:length(Varout.t)];
else
snap_idx(i) = a;
end
end
else
snap_idx = 1:length(Varout.t);
end
legenda = Varname.fvars(Varout.idx(Value(Varname.po)));
leg_value = get(Hdl_legend,'Value');
hdlab = findobj(Fig.plot,'Tag','PopupMenu3');
AbValue = get(hdlab,'Value');
Y = Varout.vars(:,Value);
if isempty(Y), return, end
% set reference angle
if ~OPF.init
ang_idx = get(Hdl_angref,'Value')-1;
if ~ang_idx
angolo = zeros(length(Varout.t),1);
else
ref_idx = get(Hdl_angref,'UserData');
ang_ref = ref_idx(ang_idx);
angolo = Varout.vars(:,ang_ref);
end
for i = 1:length(Value)
kk = Varout.idx(Value(i));
if ~isempty(find(Syn.delta == kk))
Y(:,i) = Y(:,i) - angolo;
elseif kk > nD+nB & kk <= nD+2*nB
Y(:,i) = Y(:,i) - angolo;
end
end
end
if AbValue == 1
X = Varout.t;
else
X = Varout.vars(:,AbValue-1);
end
tipoplot = get(Hdl_tipoplot,'Value');
if out_text
plainfile = fm_filenum('txt');
fid = fopen([Path.data,plainfile,'.txt'],'wt');
if fid == -1
fm_disp('Cannot open file. Data not saved.')
return
end
fprintf(fid,'C Legend:\n');
fprintf(fid,'C %s, ',Settings.xlabel);
for i = 1:size(Y,2)
fprintf(fid,'%s, ',legenda{i});
end
fprintf(fid,'\nC Data:\n');
fprintf(fid,[repmat('%8.5f ',1,1+size(Y,2)),'\n'],[X,Y]');
fclose(fid);
fm_disp(['Data exported to plain text file "',plainfile,'.txt"'])
end
if out_mtv
plainfile = fm_filenum('mtv');
fid = fopen([Path.data,plainfile,'.mtv'],'wt');
if fid == -1
fm_disp('Cannot open file. Data not saved.')
return
end
%fprintf(fid,'$ DATA=CURVE2D\n');
fprintf(fid,'%% xlabel = "%s"\n',Settings.xlabel);
if min(X) < max(X)
fprintf(fid,'%% xmin = %8.5f\n',min(X));
fprintf(fid,'%% xmax = %8.5f\n',max(X));
end
fprintf(fid,'\n');
if tipoplot == 3 | tipoplot == 6
fm_disp('MTV format does not support numbered plots.')
end
for i = 1:size(Y,2)
labelmtv = strrep(legenda{i},'{','');
labelmtv = strrep(labelmtv,'}','');
labelmtv = strrep(labelmtv,'_',' ');
labelmtv = strrep(labelmtv,'\','');
fprintf(fid,'%% linelabel="%s"\n',labelmtv);
switch tipoplot
case 2
linetype = rem(i-1,10)+1;
linecolor = 1;
markertype = 0;
markercolor = 1;
case 4
linetype = 1;
linecolor = 1;
markertype = rem(i-1,13)+1;
markercolor = 1;
case 5
linetype = 1;
linecolor = rem(i-1,10)+1;
markertype = rem(i-1,13)+1;
markercolor = linecolor;
otherwise
linetype = 1;
linecolor = rem(i-1,10)+1;
markertype = 0;
markercolor = 1;
end
fprintf(fid,'%% linetype=%d linecolor=%d markertype=%d markercolor=%d\n', ...
linetype,linecolor,markertype,markercolor);
fprintf(fid,'%8.5f %8.5f\n',[X,Y(:,i)]');
fprintf(fid,'\n');
end
fclose(fid);
fm_disp(['Data exported to MTV plot file "',plainfile,'.mtv"'])
end
if out_matlab
plainfile = fm_filenum('m');
fid = fopen([Path.data,plainfile,'.m'],'wt');
if fid == -1
fm_disp('Cannot open file. Data not saved.')
return
end
fprintf(fid,'x_label = ''%s'';\n',Settings.xlabel);
fprintf(fid,'\nvar_legend = {');
for i = 1:size(Y,2)-1
fprintf(fid,'''%s'', ',legenda{i});
end
fprintf(fid,'''%s''};\n',legenda{end});
fprintf(fid,'\noutput_data = [ ...\n');
fprintf(fid,[repmat('%8.5f ',1,1+size(Y,2)),';\n'], ...
[X(1:end-1),Y(1:end-1,:)]');
fprintf(fid,[repmat('%8.5f ',1,1+size(Y,2)),'];\n'], ...
[X(end),Y(end,:)]');
fclose(fid);
fm_disp(['Data exported to plain text file "',plainfile,'.m"'])
end
set(Fig.plot,'CurrentAxes',hdlfig);
plot(X(snap_idx),Y(snap_idx,Varname.po));
if AbValue == 1
xlabel(Settings.xlabel);
else
xlabel(Varname.fvars{AbValue-1});
end
if min(X) < max(X)
set(hdlfig,'XLim',[min(X),max(X)])
end
%legend
if leg_value == 1 | Settings.hostver >= 7
hleg = legend(legenda,0);
Hdl.legend = hleg;
set(hleg,'Color',AxesColor)
hchild = get(hleg,'Child');
set(hchild(end), ...
'FontName',AxesFont, ...
'FontWeight',AxesWeight, ...
'FontAngle',AxesAngle)
end
if tipoplot == 3 | tipoplot == 6
[quanti,tanti] = size(Y);
passo = (quanti-rem(quanti,8))/8;
colori = get(gcf,'DefaultAxesColorOrder');
for i = 1:tanti
if plot_snap
sequenza = snap_idx;
else
tmin = min(Varout.t);
tmax = max(Varout.t);
deltat = (tmax-tmin)/5;
tmin = tmin + i*(tmax-tmin)/43;
seqt = tmin:deltat:tmax;
for j = 1:length(seqt),
[valt, sequenza(j)] = min(abs(Varout.t-seqt(j)));
end
end
hdl = text(Varout.t(sequenza), ...
Y(sequenza,i),num2str(Varname.po(i)));
if tipoplot == 6,
set(hdl,'Color',colori(rem(i-1,7)+1,:));
end
end
if leg_value == 1 | Settings.hostver >= 7
hdl = findobj(Fig.plot,'Tag','legend');
oldh = gca;
set(gca,'HandleVisibility','off')
set(hdl,'Interruptible','on')
h = findobj(hdl,'Type','line');
for i = 1:tanti
j = i*2;
xdata = get(h(j),'XData');
ydata = get(h(j),'YData');
htext = text((xdata(2)-xdata(1))/2,ydata(1), ...
int2str(tanti-i+1));
set(htext,'Color',get(h(j),'Color'));
end
set(oldh,'HandleVisibility','on')
set(Fig.plot,'CurrentAxes',oldh);
end
elseif tipoplot == 4 | tipoplot == 5
[quanti,tanti] = size(Y);
passo = (quanti-rem(quanti,8))/8;
hold on
simboli = {'o';'s';'d';'v';'^';'<';'>';'x'};
colori = get(Fig.plot,'DefaultAxesColorOrder');
for i = 1:tanti
if plot_snap
sequenza = snap_idx;
else
tmin = min(Varout.t);
tmax = max(Varout.t);
deltat = (tmax-tmin)/5;
tmin = tmin + i*(tmax-tmin)/43;
seqt = tmin:deltat:tmax;
for j = 1:length(seqt),
[valt, sequenza(j)] = min(abs(Varout.t-seqt(j)));
end
end
set(hdlfig,'LineStyle',simboli{rem(i-1,8)+1});
hmarker = plot(Varout.t(sequenza), ...
Y(sequenza,Varname.po(i)));
set(hmarker,'MarkerSize',7, ...
'MarkerFaceColor',AxesColor);
if tipoplot == 5,
set(hmarker,'Color',colori(rem(i-1,7)+1,:));
end
end
hold off;
if leg_value == 1 | Settings.hostver >= 7
hdl = findobj(Fig.plot,'Tag','legend');
set(Fig.plot,'CurrentAxes',hdl);
h = findobj(hdl,'Type','line');
for i = 1:tanti
j = i*2;
xdata = get(h(j),'XData');
ydata = get(h(j),'YData');
set(hdl,'LineStyle',simboli{rem(tanti-i,8)+1});
if Settings.hostver >= 7
hmarker = plot(hdl,(xdata(2)-xdata(1))/1.2,ydata(1));
else
hmarker = plot((xdata(2)-xdata(1))/1.2,ydata(1));
end
set(hmarker,'MarkerSize',7, ...
'Color',get(h(j),'Color'), ...
'MarkerFaceColor',AxesColor);
end
set(Fig.plot,'CurrentAxes',hdlfig);
end
end
if get(Hdl_grid,'Value'); grid on; end
if ~get(Hdl_legend,'Value') & Settings.hostver >= 7
legend(Hdl.axesplot,'hide')
end
set(get(hdlfig,'XLabel'), ...
'FontName',AxesFont, ...
'FontWeight',AxesWeight, ...
'FontAngle',AxesAngle, ...
'FontSize',AxesSize, ...
'FontUnits',AxesUnits)
set(hdlfig, ...
'FontName',AxesFont, ...
'Color',AxesColor, ...
'FontWeight',AxesWeight, ...
'FontAngle',AxesAngle, ...
'FontSize',AxesSize, ...
'FontUnits',AxesUnits)
if Fig.line > 0, fm_plot('createlinelist'), end
if get(hdl_x, 'Value'), fm_plot('axesx'), end
if get(hdl_y, 'Value'), fm_plot('axesy'), end
if get(hdl_xy,'Value'), fm_plot('axesxy'), end
case 'export' % export the figure to file
tag = get(gcbo,'Tag');
SSize = get(0,'ScreenSize');
Sratio = SSize(3)/SSize(4);
axs_pos = get(Hdl.axesplot,'Position');
fig_pos = get(Fig.plot,'Position');
pap_pos = get(Fig.plot,'PaperPosition');
pap_siz = get(Fig.plot,'PaperSize');
leg_value = get(Hdl_legend,'Value');
if leg_value
pos_leg = get(Hdl.legend,'Position');
end
shrink = 0.8; % axes scale factor
set(Hdl.axesplot,'Position',[0.13 0.11 0.855 0.875])
set(Fig.plot,'Position',[fig_pos(1), fig_pos(2), ...
fig_pos(3)*shrink, fig_pos(4)*shrink])
if leg_value
pos_leg2(1) = 0.13 + 0.855*(pos_leg(1) - axs_pos(1))/axs_pos(3);
pos_leg2(2) = 0.11 + 0.875*(pos_leg(2) - axs_pos(2))/axs_pos(4);
pos_leg2(3) = pos_leg(3)*0.855/axs_pos(3);
pos_leg2(4) = pos_leg(4)*0.875/axs_pos(4);
set(Hdl.legend,'Position',pos_leg2);
if pos_leg2(1)+pos_leg2(3) > 0.985
Resize = (pos_leg2(1)+pos_leg2(3))/0.985;
fig_pos2 = [0.13 0.11 0.855 0.875];
fig_pos2(3) = fig_pos2(3)/Resize;
fig_pos2(1) = fig_pos2(1)/Resize;
pos_leg2(3) = pos_leg2(3)/Resize;
pos_leg2(1) = pos_leg2(1)/Resize;
set(Hdl.axesplot,'Position',fig_pos2)
set(Hdl.legend,'Position',pos_leg2)
end
end
if Settings.hostver > 5.3,
set(Fig.plot,'PaperSize',[pap_siz(1)*shrink, pap_siz(2)*shrink])
end
ppos(3) = pap_pos(3)*shrink;
ppos(4) = pap_pos(4)*shrink;
ppos(1) = (pap_siz(1)-ppos(3))/2;
ppos(2) = (pap_siz(2)-ppos(4))/2;
set(Fig.plot,'PaperPosition',ppos)
ax2_pos = get(Hdl.axeslogo,'Position');
set(Hdl.axeslogo,'Position',[10 10 0.2 0.2]);
Hdl_all = get(Fig.plot,'Children');
idx = find(Hdl_all==Hdl.axesplot);
if idx, Hdl_all(idx) = []; end
idx = find(Hdl_all==Hdl.axeslogo);
if idx, Hdl_all(idx) = []; end
if leg_value,
idx = find(Hdl_all==Hdl.legend);
if idx, Hdl_all(idx) = []; end
end
set(Hdl_all,'Visible','off');
warning off
lastwarn('')
switch tag
case 'PushEPS'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -