Plotting multiple figures in Matlab - matlab
I am working with some matlab code that processes data (in a Kalman Filter) and creates a series of contour plots. It has been running on a RHEL 4 server in matlab 2006a for a few years, but my boss recently requested that all servers be updated to RHEL 6...and at least matlab 2007a. I have worked out all the depreciations between these versions, but I am still having one major problem.
The code that creates and prints different contour plots is working for whichever of the three plots is created first. It looks like this:
Unfortunately the next two plots look like this:
The three figures are plotted independently in separate functions and I use clf("reset"); before and after creating each figure. Each function works in and of itself, but when all three are plotted the second and third figures are all messed up. Has anyone else had this problem? Here is the code that creates one of the figures.
function Crd = TEC_plot(ITEC,RT,Param,Input,t,OutPath,RxExtAll,Time)
% Generate TEC plot
Function_for_Spline_Smoothing = [.05 .1 .05; .1 .4 .1; .05 .1 .05];
ITEC = conv2(ITEC,Function_for_Spline_Smoothing,'same'); % add more of these lines to make contours smoother
ITEC = conv2(ITEC,Function_for_Spline_Smoothing,'same'); % add more of these lines to make contours smoother
% ITEC = conv2(ITEC,Function_for_Spline_Smoothing,'same'); % add more of these lines to make contours smoother
% ITEC = conv2(ITEC,Function_for_Spline_Smoothing,'same'); % add more of these lines to make contours smoother
earth('CAMERA',RT.Camera,'FIG',1);
figure;
warning off; hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Changed 13 February 2007 to make plots prettier
thinning_scale=2; % (1 to 10) increase this value to thin the density of the contour number labels
[cscale,hgt]=m_contour(Param.Grid.LonAxis,Param.Grid.LatAxis,ITEC, ...
round(RT.Levels(1:thinning_scale:end)/5)*5);
hold on
m_contourf(Param.Grid.LonAxis,Param.Grid.LatAxis,ITEC,RT.Levels);
shading flat
m_coast('line','color','y','LineWidth',1);
clabel(cscale,hgt,'labelspacing',72,'rotation',0,'fontsize',10 ...
,'FontAngle','italic','color','w');
axis([-.65 .6 .25 1.32]) % hardwiring axis length since the coastline runs off of the plot
% Changed 13 February 2007
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %-------------------- to include different station markers for different sources -------------------
if ~isempty(Input.Data) % Plot receivers used in this inversion
Crd=uniquerows(double(cat(1,RxExtAll)));
RxType=round(Crd(:,4));
Crd=cartsph(Crd)*180/pi;
i1 = find( (Crd(:,3) > RT.Camera(3)).*(Crd(:,3) < RT.Camera(4)).*...
(Crd(:,2) > RT.Camera(1)).*(Crd(:,2) < RT.Camera(2)).*...
(RxType==1) );
i2 = find( (Crd(:,3) > RT.Camera(3)).*(Crd(:,3) < RT.Camera(4)).*...
(Crd(:,2) > RT.Camera(1)).*(Crd(:,2) < RT.Camera(2)).*...
(RxType==2) );
i3 = find( (Crd(:,3) > RT.Camera(3)).*(Crd(:,3) < RT.Camera(4)).*...
(Crd(:,2) > RT.Camera(1)).*(Crd(:,2) < RT.Camera(2)).*...
(RxType==3) );
m_plot(Crd(i1,3),Crd(i1,2),'ro','markersize',5,'LineWidth',2);
% m_plot(Crd(i1,3),Crd(i1,2),'r.','markersize',6,'LineWidth',2);
m_plot(Crd(i2,3),Crd(i2,2),'r^','markersize',5,'LineWidth',2);
% m_plot(Crd(i2,3),Crd(i2,2),'r.','markersize',6,'LineWidth',2);
m_plot(Crd(i3,3),Crd(i3,2),'rp','markersize',5,'LineWidth',2);
plot(-.6,.45,'ro','markersize',5,'LineWidth',2);text(-.55,.45,'CORS','Color','k')
plot(-.6,.39,'r^','markersize',5,'LineWidth',2);text(-.55,.39,'GPS/Met','Color','k')
plot(-.6,.33,'rp','markersize',5,'LineWidth',2);text(-.55,.33,'RTIGS','Color','k')
end
% % -------------------------------------------------------------------------------
hold off; warning on;axis off;
% caxis([RT.Levels(1),(RT.Levels(length(RT.Levels)))/2]); colorbar('vert'); %Color bar from 0 to 50 --- for Low Solar Activity
caxis([RT.Levels(1),(RT.Levels(length(RT.Levels)))]); colorbar('vert'); %Color bar from 0 to 100 --- for High Solar Activity
title(sprintf('Total Electron Content Units x 10^1^6 m^-^2'),'Fontsize',11)
if size(Crd,1)==0,
title(sprintf('Total Electron Content Units x 10^1^6 m^-^2 Caution: No Data Available, IRI95 Only'),'Fontsize',10)
end
whitebg('w')
text(-0.6,0.22,sprintf('%s from %s to %s UT NOAA/SWPC Boulder, CO USA (op.ver. 1.0)',datestr(Time(t)+1E-8,1),datestr(Time(t)+1E-8,15),datestr(Time(t)+1E-7+15/1440,15)),'Fontsize',11)
whitebg('w')
% This option print to a file
% set(gcf, 'Position', [0,0,1950,1467]);
% print('-f1','-dpng','-painters',filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC'],Time(t)));
% system(['convert ', filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.png'],Time(t)),' -scale 650x489 ',' -colors 256 ', filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.png'],Time(t))]);
% Printing a postscript file because requirements for the automatic reboot
print('-f1','-dpsc','-r1000','-painters', filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC'],Time(t)));
% Convert the postscript file to jpg using ghostscripts
system(['gs -q -dBATCH -dNOPAUSE -r300 -sDEVICE=jpeg -sOutputFile=',filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.jpg'],Time(t)),' ', filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.ps'],Time(t))]);
% Converting from jpg to png and reducing the size of the figure.
system(['convert ',filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.jpg'],Time(t)),' -crop 2050x1675+325+775 ',' -scale 650x489 ',' -colors 256 ', filename([OutPath,'{YYYY}{MM}{DD}{HRMN}_TEC.png'],Time(t))]);
% Removing the jpg and ps files (ask Cliff how can we put both files in just one command)
%system(['rm ',filename([OutPath,'*.jpg'],Time(t))]);
%system(['rm ',filename([OutPath,'*.ps'],Time(t))]);
end
Try to not use clf which will clear your whole figure but use the figure in between to open three independant figures. See it it'll work :)
Edit: That's just guessing without actually seeing your code
One guess might be with the code that generates your final PNG file. Your second figure appears to be incorrectly cropped when converting from JPG to PNG.
First the print command is fixed to use whatever is in figure 1. So, to ensure that you are plotting using figure 1 use the command figure(1) to specify this. Alternatively, change the print command to to read print(gcf ... to print the current figure.
Also, note you could generate the PNG file directly with print as in
print(gcf,'-dpng','-r1000',filename(...))
and skip the conversion from PS to JPG to PNG step.
The hard-coding the figure window in the print might explain why things work independently but not together if the you have multiple figure windows available. Just a guess.
Related
How can I represent the vectors below as box plot?
I have below these three vectors: CoverageArea_mean = [84.4735,0,21.1779,4.5211,6.4247,2.3795,2.1416,0]; %FEC = 3.8x10^-3 CoverageArea_min = [98.5128,92.5640,21.1779,21.1779,6.9007,6.9007,2.1416,2.1416]; %FEC = 3.8x10^-3 CoverageArea_max = [70.1963,0,19.0363,0.4759,5.9488,0.2380,2.1416,0]; %FEC = 3.8x10^-3 I would like to draw them as in the figure below: x = [15 15 30 30 45 45 60 60]; I am not sure If they will be fit for the box plot or not. I want to take a value from the three vectors every time and represent them as a above. Any idea or assistance, please?
1.- I call marker (as in oscilloscopes) what in MATLAB is called pointer. 2.- I have modified Davidson's function plot_candlesticks available here https://uk.mathworks.com/matlabcentral/fileexchange/9893-candlesticks?s_tid=srchtitle_financial%20charts_23 Mathworks File exchange name: candlesticks version 1.0.0.0 (1.44 KB) by Bill Davidson Bill grants permission for modifications. 3.- With the following functions and test script you can generate graphs like this The addition of text labels is a really easy task that I have started, as you can see in the test script, but ran out of time for this question so I let you finish it off, let me know if you encounter any problem. I recommend you use command text outside the supplied functions, after plotting the graph 4.- I give you 2 functions: plot_tees and plot_bars . The function you asked for is plot_tees function [hpt]=plot_tees(ax1,x,highs,lows,opens,closes,sh,col_stick) % plot_tees plots T and inverted T on supplied data points % input checks: PENDING % make sure x has no gaps and same interval % make sure all input lengths are the same : x highs lows opens closes % check amount inputs and expected formats ok middle=sh.middle hwbody=sh.hwbody hwshad=sh.hwshad xbody=sh.xbody xshad=sh.xshad npoints=length(highs); hpt=[0 0 0]; for k1=1:npoints hp1(k1,1)=plot(ax1,[.5+x(k1)-.2 .5+x(k1)+.2],[highs(k1) highs(k1)],'Color',col_stick,'LineWidth',1.1); % top -- hp1(k1,2)=plot(ax1,[.5+x(k1)-.2 .5+x(k1)+.2],[lows(k1) lows(k1)],'Color',col_stick,'LineWidth',1.1); % low -- hp1(k1,3)=plot(ax1,[.5+x(k1) .5+x(k1)],[lows(k1) highs(k1)],'Color',col_stick,'LineWidth',1.1); % the actual stick hpt=[hpt; hp1]; end hpt(1,:)=[]; end and plot_bars function [hpt]=plot_bars(ax1,x,highs,lows,opens,closes,col_down,sh,col_up) % plot_bar plots high-low bars on supplied data % input checks % make sure x has no gaps and same interval % make sure all lengths are the same :x highs lows opens closes middle=sh.middle hwbody=sh.hwbody hwshad=sh.hwshad xbody=sh.xbody xshad=sh.xshad npoints=length(x); colors=closes<opens; colors=char(colors*col_down+(1-colors)*col_up); % 'k' black 'w' white 'r' red 'b' blue or custom RGB [r g b] range [0 1] % h1=figure(1) hpt=[]; for k1=1:npoints hp1=patch(ax1,x(k1)+xbody,[opens(k1),opens(k1),closes(k1),closes(k1),opens(k1)],colors(k1)); hpt=[hpt hp1]; end end This is the TEST SCRIPT HERE : close all;clear all;clc % opns : opens clos : closes , avoiding var names too close to reserved names col_down='r' col_up='r' col_stick='k' % this is to avoid having to make changes inside each function sh.middle=0.5; sh.hwbody=sh.middle/2; sh.hwshad=sh.hwbody/10; sh.xbody=sh.middle+[-sh.hwbody sh.hwbody sh.hwbody -sh.hwbody -sh.hwbody]; sh.xshad=sh.middle+[-sh.hwshad sh.hwshad sh.hwshad -sh.hwshad -sh.hwshad]; % time reference npoints=15 % amount CONSECUTIVE days x=[1:npoints] % day points % data y_mean0=50 % DC to avoid graph 002 y=rand(npoints,1)-0.5 opns=y_mean0+cumsum(y) highs=opns+rand(npoints,1) lows=opns-rand(npoints,1) clos=opns+(highs-lows).*(rand(npoints,1)-0.5) % plot frame h1=figure(1) ax1=gca plot(ax1,[x(1)-1 x(end)+1],[y_mean0 y_mean0]) hold(ax1,'on') plot(ax1,x,zeros(1,numel(x))+y_mean0,'|','Color',[.7 .7 .7]) % days reference % plot data spans hp1=plot_tees(ax1,x,highs,lows,opns,clos,sh,col_stick); hold on grid on; % off | minor hp2=plot_bars(ax1,x,highs,lows,opns,clos,col_down,sh,col_up); % include text tags prec=7 % precision : amount total digits into string '.' not a digit str_highs=num2str(highs,prec); str_lows=num2str(lows,prec); tr1=2 % trim, in case needed : how many decimal digits to drop from right hand side str_highs(:,[end-tr1+1:end])=[]; str_lows(:,[end-tr1+1:end])=[]; % PENDING % figure properties h1.Name='graph 01'; h1.NumberTitle='off'; h1.ToolBar='auto'; % none | figure h1.MenuBar='figure'; % | figure h1.Color=[.7 .7 .7]; % figure background colour ax1.Color=[.9 .9 .9] % T-marker properties : modify T-marker properties here for k1=1:1:size(hp1,1) for k2=1:1:size(hp1,2) hp1(k1,k2).LineWidth=1.1; hp1(k1,k2).Color=[.9 .5 .3]; end end % bars properties : modify bar properties here for k1=1:1:size(hp2,2) hp2(k1).EdgeColor='none'; end % plot aditional graphs plot(ax1,x+.5,opns,'c','LineWidth',1.5) 5.- I am not using the data you supplied in the question. Instead I use randomly generated points. You should have no trouble in replacing the random data with your data. Let me know if any handicap. 5.1.- The time reference you supplied doesn't seem to match the supplied values; x has doubled values and there are more values than days 5.2.- The mean value hardly ever is exactly the average between each high and each low 5.3.- Using the data I know I could show how to do things like changing properties and including additional graphs, because with a clear time reference is available. 6.- Because you may need to change colours, bars opacity, bars edge, bars presence, and thickness of any, as well as there may be need to turn on/off each text field on graph (of each point and of all points) all handles to properties are now available. Additional Comments : 7.- MATLAB built-in colours handy here And some other commonly used custom colours Any colour is available, just specify the RGB as vector [1 1 1] range of each element [0 1] 0: black, 1: white. 8.- MATLAB built-in markers are : arrow (default) ibeam crosshair watch topl botr circle cross fleur left right top bottom and hand. One can also create custom pointers setting up figure property PointerShapeCData. But none of these pointers are useful here because they are all 16x16 only, size matters. This is why I asked in first place if the Ts had to be same size, or more reasonably, like in financial/investment/banking graphs you asked for T-markers proportional to the specified span, for each point. 9.- This question-answer has nothing to do with what is commonly known as T-graphs : 2 lists side by side to compare features. 10.- use uifigure instead of figure if you are building your app with App Builder.
How to open desired figure by dialogue box in MATLAB
Is there any possible way to open the desired figures from a lot of plotted figures in MATLAB? If it is possible with dialogue box then it will be perfect. I have like 75 figures plotted after my code but I have closed the figures at end of loops as they are too much. Is it possible to open just one figure by just entering values necessary for plotting figure in MATLAB at end of program?
One way to do this is the following: 1) You save the figures as .fig in a dedicated folder using the saveas command, e.g.: saveas(gcf,['FileName_',num2str(idx),'.fig']); where idx is the index associated with the figure number (so in 75 in the example you mentioned). For simplicity, I would save all of them in one folder. 2) You use inputdlg to create an input dialog box, where you type in the index you want. Then, you run uiopen(['FileName_',idxFromInput,'.fig']), which will display the figure. Note that the output from inputdlg is normally a string, so you don't need num2str here.
From Wikibooks: MATLAB Programming/Handle Graphics (emphasis mine): Every time you close a figure, either by using the close function OR by hitting the 'X', you can no longer access the data, and attempts to do so will result in an error. Closing a figure also destroys all the handles to the axes and annotations that depended on it. This means that once you close your 75 figures, they are gone for good. I would suggest saving all your figures to .fig file format, because this will allow you to open them later in MATLAB. Take the following example: x = linspace(0, 2*pi); % Sample data. for i = 1:3 % Loop 3 times. h = figure; % Create figure window and capture its handle. plot(i*sin(x)); % Plot some data. saveas(h, sprintf('fig%d.fig', i)); % Save figure to .fig file format. close(h); % Delete the figure. end Now you can tell MATLAB to open one of the figures using the openfig function. For example, let's open the second figure fig2.fig. Go to the Command Window and type openfig('fig2') (including the .fig extension in the file name is optional). >> openfig('fig2') ans = Figure (1) with properties: Number: 1 Name: '' Color: [0.9400 0.9400 0.9400] Position: [520 371 560 420] Units: 'pixels' Show all properties
Matlab: Intraday Time Series Plotting Issue
Can you help me with the following issue? I have a large dataset of intraday financial data. More specifically, closing stock prices of each 15 minutes for multiple days. I face a problem in plotting the timeseries of the data. Here is an example of my series: '29-Dec-2016 15:00:00' 62.8400000000000 '29-Dec-2016 15:15:00' 62.8300000000000 '29-Dec-2016 15:30:00' 62.8900000000000 '29-Dec-2016 15:45:00' 62.8550000000000 '29-Dec-2016 16:00:00' 62.8900000000000 (Closing of the market) '30-Dec-2016 09:45:00' 62.7300000000000 (Opening of the market) '30-Dec-2016 10:00:00' 62.2900000000000 '30-Dec-2016 10:15:00' 62.2400000000000 '30-Dec-2016 10:30:00' 62.0900000000000 '30-Dec-2016 10:45:00' 62.1100000000000 '30-Dec-2016 11:00:00' 62.3000000000000 '30-Dec-2016 11:15:00' 62.2300000000000 If I plot the above subsample the matlab plot will have a form like the following picture: As you can see the Matlab plotting fills in the horizontal axis with the time between the closing of the market and the opening of the market which makes the price to look "stretched". Contrary if i use an increasing observation number (e.g. 1 to 100...) the problem is removed like in the following picture: Is there a way to avoid the "stretch" of the price and still have time on my horizontal axis? Thanks in advance.
You can do this way: First plot only price data plot(price) Then set the XTickLabel: set(gca,'XTickLabel',datevector) This will set the X axes with your data You can put this inside a function function plotprices(data) datevector = data(:,1); %store dates in first column price = num2cell(data(:,2)); %store prices in second column plot(price) set(gca,'XTickLabel',datevector)
You can read the positions of the x-ticks on the plot and replace their labels with your own strings. So, assuming: a) y has the stock prices, and b) Date has the date strings, you could add the following code at the end of the second plot to get something close to what you want: % limit the x-axis such that all ticks are within the data range xlim([1, length(y)]); % read the marks of the x-ticks xt=get(gca, 'XTick'); % this would place the x tick marks at the same locations % that Matlab chose by default. If you want to place them % at some other points, just assign those points to xt, e.g. % xt = (1:10:length(y)) % replace the labels of the marks set(gca, 'XTick', xt); % rewrite this in case you modify xt set(gca,'XTickLabel',Date(xt)) BTW, a potentially simpler alternative is to use your first plot but instead of solid line, use markers only. For example, plot(Date, y, '.');
Matlab figures - remove filling from gaussian distributions
I am emulating a NAND Flash mlc and I am using hist() to display the results which are 4 gaussian distributions. The problem is that I am trying to display 8 distributions at once one the same figure and they overlap. So, I want to find a way to remove the filling inside the gaussian distributions and only keep the outline. Is there a way to do this? I have found that maybe "basic fitting" option in Matlab can do this but it is "grey" and I cannot select it on this figure. -- Well I tried adding a simple a picture which can explain everything but I could not due to my reputation. I am working in FPGA(ZedBoard) in order to create the emulation but that has nothing to do with the question I am asking. So, I get an input file with single-precision values out of ZedBoard, which then I insert in Matlab and get the figure I am talking about. I would not think that my code in Matlab could help in any way that is why I did not put it. Although, I should have mentioned that the eight distributions have almost the exact same characteristics so they appear almost on the same place. This is why I am looking a way to display them properly. These "hists" below have to appear on the same figure without overlapping on each other. Normally, when Matlab is displaying gaussian distributions with "hist" uses a blue filling which I want to remove and only leave the outlining of the distribution if possible. I hope I have given a better view of my problem with this added. fid1 = fopen('soft_without.txt'); A = textscan(fid1,'%s'); B = char(A{1}); fclose (fid1); output = typecast(uint32(hex2dec(B)),'single'); hist(output,1000); fid2 = fopen('soft_with.txt'); A = textscan(fid2,'%s'); B = char(A{1}); fclose (fid2); output1 = typecast(uint32(hex2dec(B)),'single'); hist(output1,1000);
Let's first put all the data into one dataset, rather than having a bunch of different variables: filenames = ls('*.txt'); % or whatever you do to make up your list of files data = zeros(1000, 8); %preallocate % going to use first file to set location of bins so they're all the same fid = fopen(filenames(1,:)) A = textscan(fid,'%s'); B = char(A{1}); fclose(fid); output = typecast(uint32(hex2dec(B)),'single'); [x, bins] = hist(output,1000); data(:,1) = x; % set rest to same bins for n = 2:8 fid = fopen(filenames(n,:)) A = textscan(fid,'%s'); B = char(A{1}); fclose(fid); output = typecast(uint32(hex2dec(B)),'single'); x = hist(output,bins); data(:,n) = x; end bar(bins,data); This will plot your eight sets as eight different (side by side) bars. Another route would be to plot the over the top of each other but set different edge colors and no face color (possibly also experiment with changing width). colors = jet(8); % eight colors from the jet colormap figure; hold on; for n = 1:8 bar(bins,data(:,n),'facecolor','none','edgecolor', colors(n,:)); end
matlab printing splitted plot to file
I'm creating a code to do data analysis that will run on a server. The code is supposed to spit a pdf file with 3 plots on it. I have created a code that generates the plot fig = figure; for i = 1:3 %do some calculation to find, X, Y and fit subplot(3,1,i) scatter(X,Y) hold on plot(X,fit) end print (fig, '-dpdf','fig.pdf') X, Y, and fit are calculated/imported parameters. The output of this code is a pdf document with only the last plot on it (missing the first two). How can I print all three of them to file?
I tried your code on my CPU (X,Y and fit were generated randomly) and it works fine, so the bug may come from the interaction of this snip of code with you "%do some calculations block" I would suggest to add a "hold off" command before the end of the for loop ... gus