I´m facing a problem. I open a figure-file (.fig) in Matlab that is a 2D contourf-plot. I created the file with a software that is based on matlab but has a GUI: maptools. I added Isolines in the plot. Each Isoline is labeled by me (clabel in matlab). The problem now is that I can´t set the space between the labels of the isolines so that each isoline has a lot of labels say for example 5.
in Matlab it is pretty easy to fix that. just the following way:
[Cp hp] = contourf(x,y,levels);
clabel(Cp,hp,'LabelSpacing',150);
My Question now is whether it is possible to read in the figure-file in Matlab with openfig(anyfigure.fig) and change the space between the isoline labels. In other words, is there any isoline (or isoline label) handle for a figure file that is opened with openfig()
Thank you ;)
When you use contour/contourf you are generating instances of a contour object that you can address directly. When loading in your figure, specify an output so you have the handle to your figure, which you can use with findobj to locate your contour object. This contour object is the second input to clabel.
For example:
filepath = 'somepath';
myfig = openfig(filepath);
# Assume only one contour object in the figure
hp = findobj(myfig.Children, 'Type', 'contour');
clabel([], hp, 'LabelSpacing', 150);
Which generates the following for a sample contour plot:
Note that clabel, per the documentation, does not need the contour matrix input C if you have the handle to the contour object. The contour matrix is a property of the object so MATLAB can get it for itself.
Related
I am trying to create a smaller plot within a plot in MATLAB, for example like the image of this MATLAB File Exchange Upload.
There, two figures are created and then both of them are plotted in one figure.
My problem however is that I already have two MATLAB figures from earlier simulations and I need to embed one figure into the other one, i.e., one would be small and the other plot would be big but in the same graph. Could someone suggest an easy way to do this?
This can be done using the copyobj function. You'll need to copy the Axes object from one figure to the other:
f(1) = openfig('fig1.fig');
f(2) = openfig('fig2.fig');
ax(1) = get(f(1),'CurrentAxes'); % Save first axes handle
ax(2) = copyobj(get(f(2),'CurrentAxes'),f(1)); % Copy axes and save handle
Then you can move and resize both axes as you like, e.g.
set(ax(2),'Position', [0.6, 0.6, 0.2, 0.2]);
I have some sparse data and want to plot them as markers connected by a smooth, interpolated line - like the default behaviour of Microsoft Excel.
There are solutions to this problem easily found on the internet, but I find them unsatisfactory. What they do is: plot the sparse data as one data set drawing it as markers without lines, interpolate it with a method of choice and plot the interpolation as the second data set, with lines without markers.
The problem with these tricks is that in the legend the two data sets will be listed separately. I would expect a single data set depicted in the legend as a line crossing through a marker.
Is it possible in Matlab?
If you want to plot an interpolated line there are lots of ways to do that. You can try generating an interpolated line using the matlab interp1() function.
Let's create x and y data with no NaN.
x = randn(1,10)
y = randn(1,10)
If you want 1000 data points where previously you only had a few, that's pretty easy:
x2 = min(x):(max(x)-min(x))/1000:max(x)
y2 = interp1(x,y,x2,'cubic')
and you can plot your data and spline using
plot(x,y,'r+')
hold on
plot(x2,y2,'r-')
A custom legend is straightforward when you use handle graphics. You can plot a dummy data set with a red line passing through a marker using
h(1) = plot(NaN,NaN,'r-+')
lstring{1} = 'Data';
You can then add a legend that points to this data set using
legend(h,lstring)
You'll end up with something that looks roughly like this:
The nice thing about using handle graphics (i.e. the h) is you can throw whatever data series you want into the legend as h(end+1) and lstring{end+1}.
I'm trying to draw lines across images in different subplots. The basic idea is to use an annotation, thanks to this question on SO.
Here's a script on Mathworks File Exchange but it's not working on modern versions of MATLAB, and not working on axis image.
I find the answer myself.
Refer to this file on Mathworks file exchange.
It calls for a lot of nasty tricky calculations and transformations. Courtesy to Benoît Valley
First, get config of the figure,
% get axes properties
funit=get(get(h_axes,'Parent'),'Units');
% get axes properties
aunit=get(h_axes,'Units');
darm=get(h_axes,'DataAspectRatioMode');
pbarm=get(h_axes,'PlotBoxAspectRatioMode');
dar=get(h_axes,'DataAspectRatio');
pbar=get(h_axes,'PlotBoxAspectRatio');
xlm=get(h_axes,'XLimMode');
ylm=get(h_axes,'YLimMode');
xd=get(h_axes,'XDir');
yd=get(h_axes,'YDir');
% set the right units for h_axes
set(h_axes,'Units',funit);
axesoffsets = get(h_axes,'Position');
x_axislimits = get(h_axes, 'xlim'); %get axes extremeties.
y_axislimits = get(h_axes, 'ylim'); %get axes extremeties.
x_axislength = x_axislimits(2) - x_axislimits(1); %get axes length
y_axislength = y_axislimits(2) - y_axislimits(1); %get axes length
and finally
xfigure = xab+xwb*(xaxes-x_axislimits(1))/x_axislength;
Why I don't use generate code?
Generate code gives me this sort of stuff:
% Create line
annotation(figure1,'line',[0.223214285714286 0.694642857142857],...
[0.552380952380952 0.630952380952381]);
Which is not related to existing lines, points or figures, not to mention the variables not plotted. So basically I'm looking for ways to get those point positions in that annotation coordinates, which is dealt with in the above code.
If I didn't get you guys right on how to use generate code, plz let me know.
I'm trying to do a wedge plot (right ascension vs redshift). I was thinking I could use a scatter plot in polar coordinates. The polar function in matlab seems very limited. Even this
polar(a(:,1),a(:,2),'Linewidth',1)
gives me an error:
Error using polar (line 23)
Too many input arguments.
Is there a simple way to achieve what I want using Matlab? Do you know of another software that would do it easily?
Thanks,
Mike
Matlab is quite adequate for that, I think.
As for the polar function, it seems it doesn't allow properties (such as 'linewidth') to be specified directly. But you can get a handle to the created object and then set its 'linewidth', or other properties:
h = polar(a(:,1),a(:,2));
set(h,'linewidth',1)
If you want a scatter plot, maybe you'd prefer not to have lines, but instead to plot a marker (such as a dot) at each point:
h = polar(a(:,1),a(:,2),'.');
set(h,'markersize',12)
Example:
To see a list of properties that you can set, as well as their current values, type
get(h)
I have a problem plotting a 3D plot of my transfer function. In matlab I have tryed this:
[T,w] = meshgrid(1:1:32,1:1:100);
sys2=20*log((1-w.*(T./2)./w.*T).*(((2.56.*(w.^2)+1.6.*w+1)./(0.0008.*(w.^6)+0.0124.* (w.^5)+0.173.*(w.^4)+(w.^3)))./1+(((2.56.*(w.^2)+1.6.*w+1)./(0.0008.*(w.^6)+0.0124.*(w.^5)+0.173.*(w.^4)+(w.^3))))));
surf(T,w,sys2);
But I get this error:
??? Error using ==> surf at 78
X, Y, Z, and C cannot be complex.
What could be wrong please?
Or can anyone tell me how to plot this in Mathcad?
Thank you.
You can't plot a complex number versus two independent variables -- you would need four axes.
What you can do is:
Use two separate figures (or two subplots in the same figure) to plot real part and imaginary part. In Matlab,
surf(T,w,real(sys2));
figure %// create new figure for the other graph
surf(T,w,imag(sys2));
Alternatively, plot absolute value and phase:
surf(T,w,abs(sys2));
figure %// create new figure for the other graph
surf(T,w,angle(sys2));
A more exotic possibility is to use z axis for absolute value and colour for phase, in the same graph:
surf(T,w,abs(sys2),angle(sys2)); %// fourth argument of surf specifies colour