Switch axes on .fig matlab - matlab

While editing a .fig matlab file, I want to swap the x and y axes without redrawing the plot by code (without changing the order of vector data). Does a solution exist to my problem? Is there some option in the figure properties menu? Thanks in advance.

In addition to Luis 's answer you can set the current axes View property to [90 -90] directly from the property inspector.
Programmatically this is equivalent to this:
set(gca,'View',[90 -90])
Note:
Thanks to Luis for the correction. Using [-90 90] does swap the axis but then you need to reverse the direction of the y-axis. Therefore it's better to use [90 -90].
Simple example:
Before swap:
And then after changing the view:

You can directly access the 'XData' and 'YData' properties of each plot and swap them:
c = get(gca,'children'); %// get children of axes
for n = 1:numel(children); %// for each children
set(c(n),'XData',get(c(n),'YData'),'YData',get(c(n),'XData')); %// swap XData, YData
end

Related

GCA function doen't work to change axis-labels

I try to present 8 (name) labels on my x-axis. Instead, I get number 1 to 8.
Problem: In my previous asked question, I used gca function, that allows me to change axis labels. However, the same gca function doesn't work here.
This is my MatLab output:
Instead of 1,...8, I want to see Firm1...Firm8!
This is my code:
figure(2);
%four variables:
%pi --> 8x1 vector
%E_R_BL_Idzorek --> 8x1 vector
%pi_star1 --> 8x1 vector
%ER_100_TF1 --> 8x1 vector
ALL_DATA=[pi(1,1) E_R_BL_Idzorek(1,1) pi_star1(1,1) ER_100_TF1(1,1);pi(2,1) E_R_BL_Idzorek(2,1) pi_star1(2,1) ER_100_TF1(2,1);pi(3,1) E_R_BL_Idzorek(3,1) pi_star1(3,1) ER_100_TF1(3,1);pi(4,1) E_R_BL_Idzorek(4,1) pi_star1(4,1) ER_100_TF1(4,1);pi(5,1) E_R_BL_Idzorek(5,1) pi_star1(5,1) ER_100_TF1(5,1);pi(6,1) E_R_BL_Idzorek(6,1) pi_star1(6,1) ER_100_TF1(6,1);pi(7,1) E_R_BL_Idzorek(7,1) pi_star1(7,1) ER_100_TF1(7,1);pi(8,1) E_R_BL_Idzorek(8,1) pi_star1(8,1),ER_100_TF1(8,1)];
%plotting it with a bar function
bar(ALL_DATA);
%This is where I have problem with gca function
set(gca,'xticklabel',{'Firm1','Firm2','Firm3','Firm4','Firm5','Firm6','Firm7','Firm8'});
%this is the grid part:
grid on
ll = cell(1,4);
ll{1}='pi'; ll{2}='ERidz'; ll{3}='piTF'; ll{4}='ERTF';
legend(bar(ALL_DATA),ll);
You are using a newer version of MATLAB so you should use the newer graphics system. The newer system is based on objects. This makes setting properties of things like axes easier. For example:
fh = figure; % creates the figure window save the figure handle to set it's properties
ax = axes(fh); % creates the axes in the figure, again save the object
x = rand(8,100);
h = bar(ax, x); % create the bar graph in your axes object
% now use the saved object to access the exact feature you want. This way you always have the thing you want. No searching.
ax.XTickLabel = {'Firm1','Firm2','Firm3','Firm4','Firm5','Firm6','Firm7','Firm8'};
Saving the objects is also handy for tracking legends and other things. For example: legend(ax,... You know exactly which legend you're dealing with.
What appears to be happening is that you are correctly changing the XTicks as you want but then you overwrite your graph with legend(bar(.... That creates a new bar graph. Try changing that line to just legend(ll). I would still suggest using the object system.
It seems the problem is that you redraw the bar when you run
legend(bar(ALL_DATA),ll);
You should simply do
legend(ll);

Swapping x & y Axis in Matlab

Potentially easy matlab question here, but I've searched and can't sort out how to do this.
I've got a variables, which plot like this:
I simple want the x axis to be the y axis and vice versa. How do I swap them?
Thank you in advance for your help!!
The standard way would be to swap the arguments passed to plot:
plot(ydata, xdata) %// instead of plot(xdata, ydata)
Failing that, you can change the view to rotate the axes:
view([90 -90]) %// instead of normal view, which is view([0 90])

How to specify the axis size when plotting figures in Matlab?

Suppose that I have 2 figures in MATLAB both of which plot data of size (512x512), however one figure is being plotted by an external program which is sets the axis parameters. The other is being plotted by me (using imagesc). Currently the figures, or rather, the axes are different sizes and my question is, how do I make them equal?.
The reason for my question, is that I would like to export them to pdf format for inclusion in a latex document, and I would like to have them be the same size without further processing.
Thanks in Advance, N
Edit: link to figures
figure 1: (big)
link to smaller figure (i.e. the one whose properties I would like to copy and apply to figure 1)
For this purpose use linkaxes():
% Load some data included with MATLAB
load clown
% Plot a histogram in the first subplot
figure
ax(1) = subplot(211);
hist(X(:),100)
% Create second subplot
ax(2) = subplot(212);
Now link the axes of the two subplots:
linkaxes(ax)
By plotting on the second subplot, the first one will adapt
imagesc(X)
First, you have the following:
Then:
Extending the example to images only:
load clown
figure
imagesc(X)
h(1) = gca;
I = imread('eight.tif');
figure
imagesc(I)
h(2) = gca;
Note that the configurations of the the first handle prevail:
linkaxes(h)
1.Get the handle of your figure and the axes, like this:
%perhaps the easiest way, if you have just this one figure:
myFigHandle=gcf;
myAxHandle=gca;
%if not possible, you have to search for the handles:
myFigHandle=findobj('PropertyName',PropertyValue,...)
%you have to know some property to identify it of course...
%same for the axes!
2.Set the properties, like this:
%set units to pixels (or whatever you prefer to make it easier to compare to the other plot)
set(myFigHandle, 'Units','pixels')
set(myAxHandle, 'Units','pixels')
%set the size:
set(myFigHandle,'Position',[x_0 y_0 width height]) %coordinates on screen!
%set the size of the axes:
set(myAxHandle,'Position',[x_0 y_0 width height]) %coordinates within the figure!
Ok, based on the answer of #Lucius Domitius Ahenoba here is what I came up with:
hgload('fig1.fig'); % figure whose axis properties I would like to copy
hgload('fig2.fig');
figHandles = get(0,'Children');
figHandles = sort(figHandles,1);
ax(1) = findobj(figHandles(1),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
ax(2) = findobj(figHandles(2),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
screen_pos1 = get(figHandles(1),'Position');
axis_pos1 = get(ax(1),'Position');
set(figHandles(2),'Position',screen_pos1);
set(ax(2),'Position',axis_pos1);
This is the 'before' result:
and this is the 'after' result:
Almost correct, except that the aspect ratios are still off. Does anybody know how to equalize everything related to the axes? (I realize that I'm not supposed to ask questions when posting answers, however adding the above as a comment was proving a little unwieldy!)

Save exact image output from imagesc in matlab

Hi , I want to save this image produced from imagesc(magic(3)), the exact rainbow representation, is it possible?
Thanks.
This question might look like a duplicate , but it is not . I looked at the solution to the similar question at this site , but it did not satisfy me .
I have looked into the Matlab help center and the close answer that I got was this one , at the bottom of http://goo.gl/p907wR
To save the figure as a file (don't matter how it was created), one should do:
saveas(figureHandle,'filename','format')
where figureHandle could be the gcf handle, which means: get current figure.
As pointed in the discussion, if someone doesn't want the ticks to be shown, the person can add:
set(gca,'XTick',[])
set(gca,'YTick',[])
where gca is the handle to the current axis, just as gcf. If you have more than one axis, don't forget to "handle the handles". They are returned to you when you create them, i.e.:
hFig = figure(pairValuedProperties); % Create and get the figure handle
hAxes1 = suplot(2,1,1,pairValuedProperties); % Create and get the upper axes handle
hAxes2 = suplot(2,1,2,pairValuedProperties); % Create and get the bottom axes handle
where the pair value are the figure or axes properties declared in the following syntax:
'PropertyName1',PropertyValue1,'PropertyName2',PropertyValue2,…
Here are the matlab documentation about the Figure and Axes Properties, and about the saveas method.
Example:
The image saved with the following code:
figure
imagesc(magic(3))
set(gca,'XTick',[]) % Remove the ticks in the x axis!
set(gca,'YTick',[]) % Remove the ticks in the y axis
set(gca,'Position',[0 0 1 1]) % Make the axes occupy the hole figure
saveas(gcf,'Figure','png')
You can use:
print -djpeg99 'foo.jpg'
This will save it as 'foo.jpg' as you need.
You can use the following code
imagesc(A);
%%saving the image
hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 4 4]);
print -djpeg filename.jpg -r10
Here A will be the matrix from which you will have an image. And the image will be saved as filename.jpg in the directory.

changing the values specified on the axis - matlab

I am trying to generate the following graph:
Up until now I managed to do the following:
>
close all
figure(1)
x=-12:0.3:12;
y=gaussmf(x,[3 3]);
xlim([-12,12])
plot(x,y,'go-','LineWidth',2)
y1=gaussmf(x,[3 -3]);
xlim([-15,15])
hold on
plot(x,y1,'b-s','LineWidth',2)
hold on
line([-3,-3],[1,0],'color','black','LineWidth',2);
hold on
line([3,3],[1,0],'color','black','LineWidth',2);
I didn't manage to adjust the x axis so it will mark the values -12:3:12 instead of -15:5:15.
You're wanting your tick locations in different places? You can set them manually by changing the 'Xtick' property of your graphics object, as in
set(gca,'xtick', -12:3:12)