Though there are quite a lot of answered questions to that kind of issue, I wasn’t able to find a proper solution for my exact problem. Anyway.
I try to format my tick lables like shown in the following example:
I already found out that the ‘sprintf’ command is offering a possibility to alter the tick format. The closest I came to what I want is the 'e-notation' triggered by the following command:
set(ax,'YTickLabel',sprintf('%2.0e|',yticks))
However, I’d like my labels to appear just as shown in the example picture. Is there a simple way to do that?
Thank you very much in advance,
Joe
You could use Latex formatting and sprintfc to produce what you want. (You might not need sprintfc at all actually but that's a nice way of creating a cell array of strings with numbers in a single line.):
set(ax,'YTickLabels',sprintfc('10^{%i}',yticks)
In a general example (here with x-axis formatted):
clear
clc
close all
x = 0:100000;
y = log(x);
figure
semilogx(x,y)
xt = get(gca,'XTick');
set(gca,'XTickLabels',sprintfc('10^{%i}',0:numel(xt)-1))
outputs the following:
Am I missing something? Why not use semilogy?
x = -3:0;
y = 10.^x;
semilogy(x, y);
set(gca, 'YMinorGrid', 'on')
Related
edit: I figured it out and don't see a place to mark this as answered. Thanks for the suggestions though everyone!
A couple of weeks ago I was trying to force MATLAB to display a y = 0 line for a plot I was making. It was easy enough to search for, but apparently I made it automatic. Now I can't find anything even remotely similar to this new problem. When I run this code:
plot(x,y_known,x,y_simulated);
legend('Simulated','This stupid line right here','Known')
I get the following:
Notice the line at y = 0, it is not in the code. I wouldn't really care, but it is the second line in the figure and it messes up my ability to create a legend. i.e., if the legend entry was just:
legend('Simulated','Known')
then the legend would say that the known value was green, which is certainly not the case.
I don't really want to create a handle for every single line I plot in the future, and would much rather just get rid of this line. Can anyone provide some help, or at least point me in the right direction?
edit: The y = 0 line also changes its line properties based on whatever is supplied to the first plot entry. So plot(x,y1,'--',x,y2); makes both y1 and y = 0 dashed, but plot(x,y1,x,y2,'--'); would just render the second line dashed
As an absolute last resort (after failing to find how the line actually gets there), what you can do is access the Children property of your axes and delete the child which is the line you don't want.
Something along the lines of:
ch = get(gca,'Children');
delete(ch(2)); %// Where 2 should be replaced by the child index you're trying to delete.
There was an errant two much earlier in the code, giving a variable 2 columns instead of one. I didn't realize Matlab would be helpful be helpful and plot both columns (I've always explicitly told it to plot both). Just another case of someone not thinking in vector mode when using Matlab!
What I have:
hold on
for i =1:length(tspan)
var{i} = Tv(:,i);
str{i} = ['t = ',num2str(tspan(i)), ' s'];
plot(z,var{i},'DisplayName',str{i});
end
legend('-DynamicLegend');
This works perfectly (thanks to this), but it prints out all blue lines.
I tried to set up a colormap (the default) and use it like this, but the output was the same
plot(z,var{i},'DisplayName',str{i},'Color', colormap(i,:));
And I would also like to see different markers for each plot. How is it possible to change them?
EDIT
Thanks to ironzionlion I fixed the colors. How can I do the same with markers?
According to the post that you mention, you have to define that you will need i different colours in your plot. This can be done by using colors = hsv(i)
Your plot sentence will then be: plot(z,var{i},'DisplayName',str{i},'Color', colors(i,:));
Update
I am not aware of the existance of "markermap". You could fix the problem just by define upfront the different markers that you want (quick and dirty solution): mrk={'o','+','*','.'};
Then you will plot by selecting each time the corresponding marker:
plot(z,var{i},'DisplayName',str{i},'Color', cmap(i,:),'Marker', mrk{i});
I'm currently trying to make a GUI that will allow a user to select a range of x-values, limited to a set of predefined "markers" that can appear on the graph of some data. The Matlab program has a bunch of data that's already delimited with some number of markers, and will ask the user to choose two of these markers as a start and stop point, and then continue from there.
My question is whether or not Matlab has a built-in function or object that will place some kind of interactive marker on the plot (preferably on the bottom of the graph so that it doesn't obscure the data) that the user can click on so that I can get a call-back function from it and see which marker the user chose (and also perhaps have the ability to change its color and such to represent its selection).
Preferably the answer will not involve any add-ons, but any answer and any help would be greatly appreciated. Thank you!
Here is a very simple example using ginput, which asks the user to select a starting and ending points from which to plot the data.
clear
clc
close all
x = 1:15*pi;
figure
plot(x,sin(x),'LineWidth',2);
uiwait(msgbox('Select a start and finish point'))
a = zeros(1,2);
[a,~] = ginput(2);
xStart = a(1);
xFinish = a(2);
set(gca,'XLim',[xStart xFinish],'XTick',round(xStart):1:round(xFinish))
Is it something like this you had in mind? Do you really need a callback or is this sufficient? If not could you elaborate on what kind of markers you would need?
Hope that helps!
I try to remove the Matlab-given units from this plot but I don't find a way:
figure(1)
hold on
set(gcf,'PaperUnits','centimeters',...
'PaperSize',[15 9],...
'PaperPosition',[0 0 15 9]);
pzmap(LB); sgrid; grid on; axis equal;
title('');
xlabel('\sigma [rad/s]')
ylabel('\omega [rad/s]')
hold off
After that commands the xlabel looks like this: \sigma [rad/s] (seconds^-1). The seconds comes with pzmap. How can I remove them?
I found, some strange behavour:
If generate code by the figure plot manager I get this:
% Create xlabel
xlabel('\sigma [rad/s] (seconds^{-1})','Units','pixels');
Why???
Now I get it - without pzmap/pzplot
pol = pole(sys)
figure(1)
plot(real(pol(:)),imag(pol(:)),'x')
title('');
xlabel('\sigma [rad/s]');
ylabel('\omega [rad/s]');
sgrid
pzmap is a high-level convenience function, but it's not the best choice for this (it's also stored in a folder of obsolete functions in R2013a, so it may get marked for official removal in the future). Instead, let's create an example plot using pzplot directly instead of pzmap. This is still a plot function that does a lot under the hood, but it returns a handle, h, to the plot:
sys = rss(3,2,2);
h = pzplot(sys);
sgrid;
axis equal;
We can via the options of a pzplot with getoptions:
p = getoptions(h)
To set the labels and units as you desire, you might try this, using setoptions:
p.Title.String = '';
p.XLabel.String = '\sigma';
p.YLabel.String = '\omega';
setoptions(h,p);
I believe that the units of 'seconds-1' that the plot displays is equivalent to the 'rad/s' that you want to specify. I know that the two look is very different (I prefer being specific about radians myself), but that's a disadvantage of using such a plot function that tries to do everything for you. If you wanted to remove the default string or add another option, you'd likely have to do some low level hacking. An easier way around, might be to use the "Generate Code..." command ("Generate M-File..." in older versions") under the "File" menu in the figure's toolbar and edit the plot labels there (there's also a programmatic option for this on the File Exchange). Or you could output to postscript and edit that.
Alternatively, you can use pzoptions to create a list of options to pass to pzplot or pzmap (undocumented in the latter case):
p = pzoptions;
p.Title.String = '';
p.XLabel.String = '\sigma';
p.YLabel.String = '\omega';
sys = rss(3,2,2);
pzplot(sys,p);
sgrid;
axis equal;
You'll see that that for some reason the text size is much smaller in this case. pzplot and pzmap must set the font size to 10 themselves. You could easily do this.
Fore more on customizing this and related Control toolbox plots, see this article.
After intense low-level digging, there is actually a pretty simple way to override the default behavior.
p = pzplot(sys);
p.AxesGrid.XUnits = 'rad/s';
p.AxesGrid.YUnits = 'rad/s';
Changes appear to take effect immediately. I have even tried setting the value to nothing, i.e.
p.AxesGrid.XUnits = '';
and it effectively removes the annoying parenthesis with the units. Technically, matlab creates a custom-class element they store under the name AxesGrid in the resppack.mpzplot class instance, with some standard LTI-behavior. You can probably work around some stuff by "injecting" a script with the same name as one of the standard library functions, so that it will be called instead, and change things in there, but this is the closest I have come to removing those annoying units in a few lines.
As a side info, the AxesGrid object is initialized in
...\controllib\graphics\#resppack\#pzplot\initialize.m
should you want to check it out.
I've a little problem on my graph when using core-plot.
I plot my datas using 2 arrays, 1 for Y axe and 1 for X axe, classic.
My problem is that I have values like this:
Values Array : (
"0.105814",
"0.105828",
"0.1058",
"0.105814",
"0.1058",
"0.105793",
"0.105779",
"0.10575",
"0.10575",
"0.10558"
And when I display the graph, I just see that:
And I don't want one "0,1" but the entire value. I didn't find the parameter so if someone know, I guess it's not really complicated.
Thanks for any help!
PS: And I know it's not "label" like labels are used in core-plot but I don't know how to call it :P
And sorry for my English.
Each axis has a property called labelFormatter. This is a standard NSNumberFormatter. You can create a new formatter that formats the labels any way you want. See Apple's docs for details.