My Octave workflow is the following:
I have tons of data to process, and lots of plots to generate. For each plot,
I have a function that does all the work, generates its own plot and returns the handle of this plot for future modifications (if needed). Something like this:
function [h,p] = processData_and_generatePlot_A(datainput)
%%.....
h = figure();
p = plot(h, ...)
%%....
end
Now, what I'm trying to do is a script calling all this functions, collecting all the figures, and trying to combine all of them in only one figure (i.e., each plot generated should be a subplot in a new figure).
So, the questions are:
Is it possible to pass the plot handler to the subplot function, so the plot is printed instead of generate a new one?
If not, is there any way to insert existing plots into a new figure?
Thanks in advance
A method for merging plots in different figures as subplot of a new figure actually exists. What afraids me is that you have "lots of plots to generate", so you must define a criterion for splitting the existing plots into N figures in order to avoid cramming all of them into a single figure.
The aforementioned approach involves the usage of the copyobj function, and here is an example that you can easily modify following your needs:
f1 = figure();
x1 = -10:0.1:10;
y1 = sin(x1);
p1 = plot(x1,y1,'r');
f2 = figure();
x2 = -10:0.1:10;
y2 = cos(x2);
p2 = plot(x2,y2,'r');
pause(5);
f3 = figure();
sub1 = subplot(1,2,1);
sub2 = subplot(1,2,2);
copyobj(p1,sub1);
delete(f1);
copyobj(p2,sub2);
delete(f2);
Related
I need to prevent a specific plot entry from being displayed on a Matlab plot legend.
Sample:
% x and y are any plot data
for i=1:5
plot(x,y);
plot(x2,y2,'PleaseNoLegend!'); % I need to hide this from legend
end
legend('show');
Is there any flag I can set inside the plot command so this specific entry doesn't show up in legend?
You can achieve that by setting the 'HandleVisibility' property to 'off'. Note that this hides the handles of those plots to all functions, not just to legend.
For example,
hold on
for k = 1:3
x = 1:10;
y = rand(1,10);
x2 = x;
y2 = y + 2;
plot(x,y);
plot(x2,y2,'--','HandleVisibility','off'); % Hide from legend
end
legend('show')
produces the graph
You can use the semi-documented function called hasbehavior, that allows you to ignore individual plots in a legend after you issued the plot command.
figure;
hold on;
for i=1:5
plot(x,y);
h = plot(x2,y2);
hasbehavior(h,'legend',false);
end
legend('show');
The fact that it's semi-documented suggests that it could break sooner or later in a newer MATLAB version, so use with care. It might still be a convenient choice for certain applications.
As #stephematician noted, this MATLAB built-in is also unavailable in Octave, which might be another reason why the other answers are preferable.
As Luis Mendo mentions (and I somehow missed this) the handle is hidden to all other functions in his answer, which will be ok in most situations, but an alternative solution which looks identical to the above and doesn't have this effect is:
k_values = 1:3;
h = nan(size(k_values));
x = 1:10;
hold on
for k = k_values
y = rand(size(x));
y2 = y + 2;
h(k) = plot(x,y);
plot(x,y2,'--');
end
hold off
legend(h, strcat('data', num2str(k_values')))
The final command sets the legend entry for each handle returned by the plot(x,y) command. The first argument is a 1x3 array of line handles which will appear in the legend, and the second argument is a 3x5 char matrix where each row is a label.
I want to update a plot with multiple data lines/curves as fast as possible. I have seen some method for updating the plot like using:
h = plot(x,y);
set(h,'YDataSource','y')
set(h,'XDataSource','x')
refreshdata(h,'caller');
or
set(h,'XData',x,'YData',y);
For a single curve it works great, however I want to update not only one but multiple data curves. How can I do this?
If you create multiple plot objects with a single plot command, the handle returned by plot is actually an array of plot objects (one for each plot).
plots = plot(rand(2));
size(plots)
1 2
Because of this, you cannot simply assign another [2x2] matrix to the XData.
set(plots, 'XData', rand(2))
You could pass a cell array of new XData to the plots via the following syntax. This is only really convenient if you already have your new values in a cell array.
set(plots, {'XData'}, {rand(1,2); rand(1,2)})
The other options is to update each plot object individually with the new values. As far as doing this quickly, there really isn't much of a performance hit by not setting them all at once, because they will not actually be rendered until MATLAB is idle or you explicitly call drawnow.
X = rand(2);
Y = rand(2);
for k = 1:numel(plots)
set(plots(k), 'XData', X(k,:), 'YData', Y(k,:))
end
% Force the rendering *after* you update all data
drawnow
If you really want to use the XDataSource and YDataSource method that you have shown, you can actually do this, but you would need to specify a unique data source for each plot object.
% Do this when you create the plots
for k = 1:numel(plots)
set(plots(k), 'XDataSource', sprintf('X(%d,:)', k), ...
'YDataSource', sprintf('Y(%d,:)', k))
end
% Now update the plot data
X = rand(2);
Y = rand(2);
refreshdata(plots)
You can use drawnow:
%Creation of the vectors
x = 1:100;
y = rand(1,100);
%1st plot
h = plot(x,y);
pause(2);
%update y
y = rand(1,100);
set(h,'YData',y)
%update the plot.
drawnow
I have saved different Matlab plots in an unique .fig. The figure is like this:
Now, I would like to introduce a filter in these plots to reduce the noises, but unfortunately I have lost the code that generates these signals.
Is there a way to extract data of each signal in this figure?
I tried this:
open('ttc_delay1000.fig');
h = gcf; %current figure handle
axesObjs = get(h, 'Children'); %axes handles
dataObjs = get(axesObjs, 'Children'); %handles to low-level graphics objects in axes
objTypes = get(dataObjs, 'Type'); %type of low-level graphics object
xdata = get(dataObjs, 'XData'); %data from low-level grahics objects
ydata = get(dataObjs, 'YData');
But I am confused and I don't know if it's the right way to act.
Thanks!
A one-liner for your problem:
data = get(findobj(open('ttc_delay1000.fig'), 'Type','line'), {'XData','YData'});
The steps are there (from the inner calls to the outer calls):
open the file;
look into it for the line series;
return the data.
data{n,1} will contain the XData of the LineSeries number n, wile the data{n,2} will contain the YData of the said LineSeries.
If you want to smooth the lines directly in the figure, the idea is the same:
%//Prepare moving average filter of size N
N = 5;
f = #(x) filter(ones(1,N)/N, 1, x);
%//Smooth out the Y data of the LineSeries
hf = open('ttc_delay1000.fig');
for hl = transpose(findobj(hf,'Type','line'))
set(hl, 'YData', f(get(hl,'YData')));
end;
saveas(hf, 'ttc_delay1000_smooth.fig');
I am looking for a way to merge two Matlab plots. I have the figure files for each of them as fig1.fig and fig2.fig One figure contains a plot which runs for a certain range e.g 1 to 100 and the other figure contains the continuation of the first plot e.g 101 to 200. Each of these plots takes around 8 hours, so I do not want to replot them. Is there any simple way of merging these two plots?
It sounds like you want to join up your data, so you need to extract the x and y data from each of your plots. If you have a line plot, you can load the first .fig file
e.g.
and then type
a = gca
handles = findobj(a)
isLine = strcmp(get(handles, 'Type'), 'line')
XData1 = get(handles(isLine), 'XData')
YData1 = get(handles(isLine), 'YData')
That will extract the x and y data for your line, from your first plot.
Now close all your figures and load your second plot:
a = gca
handles = findobj(a)
isLine = strcmp(get(handles, 'Type'), 'line')
XData2 = get(handles(isLine), 'XData')
YData2 = get(handles(isLine), 'YData')
You can now plot your merged plot with:
figure
plot([XData1 XData2], [YData1 YData2])
title('mergedPlot')
So I have a simple loop in MATLAB that does the following:
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
figure(1)
plot(randn(1,100));
figure(2);
plot(randn(1,100));
end
The x and y are made up, but that is the jist of it. Anyway, when I run this code, not surprisingly, MATLAB will make two figures and plot accordingly. The problem is, I get a sort of 'blinking' between figures when I do this, and it makes the quality of seeing x and y evolve over time poorer.
I discovered a way to make one of the plots smoother like this:
figure(1);
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
plot(randn(1,100));
drawnow
end
If I do this, then of course figure(1) will plot very smoothly showing x nicely, without figure(1) 'blinking' between plots, but now I cant show figure(2) or y!
How can I plot both those quantities on different figures (not subplots) smoothly without 'blinking'?
EDIT:
Thanks Geodesic for your answer, the solution works, however there is a subtlety that I did not think would be an issue, however it is.
1) I am unable to use 'imagesc' with this solution.
For example,
figure(1);
aone = axes;
figure(2);
atwo = axes;
for p = 1:100
x = 4.*randn(1,100);
y = 7.*rand(10,100);
plot(aone,x);
drawnow;
imagesc(atwo,y);
drawnow;
end
In this case the part with imagesc(atwo, y) crashes.
Your flicker is because you're generating each figure window again and again through the loop, which is forcing the window to come to the foreground each time. Generate the figures first, attach some axes to them, and plot your data to each axis like so:
figure(1);
aone = axes;
figure(2);
atwo = axes;
for p = 1:100
x = 4.*randn(1,100);
y = 7.*randn(1,100);
plot(aone,randn(1,100));
drawnow;
imagesc(y,'Parent',atwo);
drawnow;
end
Edit: functions like plot take an axis argument directly, but imagesc does not. In this particular case you'll need to send a Property Name/Value pair in as an argument. The 'Parent' of the image generated will be our axis atwo (see above).
For p = 1, create the plots you need, using the plot command or the imagesc command. Keep the handle of the resulting graphics object by getting an output argument: for example h = plot(.... or h = imagesc(..... This will be a Handle Graphics lineseries or image object, or something else, depending on the particular plot type you create.
For p = 2:100, don't use the plotting commands directly, but instead update the relevant Data properties of the original Handle Graphics object h. For example, for a lineseries object resulting from a plot command, set its XData and YData properties to the new data. For an image object resulting from an imagesc command, set its CData property to the new image.
If necessary, call drawnow after updating to force a flush of the graphics queue.