Matlab plot numbering error - matlab

When creating a plot in Matlab the following error is produced:
I have tried many different methods of trying to find the problem and solving it. I think I have identified that the cause is because I am using multiple xlim commands. Here is my code:
figure
plot(datee,[allfile.bytes],'k','LineWidth',1.5);
hold on;
plot(datee,EMA,'--b','LineWidth',0.75);
title(sprintf('File size of %s',[token{1},dateno{1},name]));
xlabel('Month/Day');
set(gca, 'YTickLabel', num2str(get(gca,'YTick')'/1024,'%0.0f'));
datetick('x','mm/dd');
ylabel('Bytes in Kb');
plot(xlim,[ulimit ulimit], 'r')
plot(xlim,[ASize{jj} ASize{jj}], 'g')
plot(xlim,[llimit llimit], 'r')
axis tight;
axis 'auto y';
hlegend=legend('File Size','Average/EMA','Upper/Lower limit','Current Size');
set(hlegend,'Location','Best')
I do not understand why the problem occurs as it does.. And also how to solve it! Has anyone experienced this before? Since I think the xlim command is the fault, are there any other alternatives to creating a solid straight line across the whole plot?

Okay, I am unsure as to why this works, but I changed the code order very slightly to this:
figure
plot(datee,[allfile.bytes],'k','LineWidth',1.5);
hold on;
plot(datee,EMA,'--b','LineWidth',0.75);
title(sprintf('File size of %s',[token{1},dateno{1},name]));
xlimit = get(gca,'XLim');
plot(xlim,[ulimit ulimit], 'r')
plot(xlim,[ASize{jj} ASize{jj}], 'g')
plot(xlim,[llimit llimit], 'r');
xlabel('Month/Day');
set(gca, 'YTickLabel', num2str(get(gca,'YTick')'/1024,'%0.0f'));
datetick('x','mm/dd')
ylabel('Bytes in Kb');
axis tight;
axis 'auto y';
And it no longer reproduces the above bug... I have no idea why though.
If anyone ever has any suggestions/ideas, please let me know, I am very curious. Hope this helps anyone with the same problem in the future.

Related

Matlab: 2 non-continuous colorbars and colormaps in the same figure

I'm completely stranded!!!
Could anyone give me a hand with this issue? Thanks in advance.
What I wanted: I want to plot a brain network such that nodes have their own colormap (jet) and colorbar and, in the same figure, the links with their own colormap (gray) and colorbar.
Problem: When I display on screen the head with two colorbars from two different colormaps, both colorbars display with the first declared colormap (jet) in my code. In other words, it seems that the first declared colormap (jet) re-writes the last declared colormaps (gray). The figure displayed shows the first colormap (jet) and never shows an independent colorbar gray!!!
What have I done?: I had follow so many links through these forums and mathworks helps to build my own script. I'm naive in Matlab and I using it in macOS with matlab version R2013a. The code looks like this:
Figure
hold
plot1 with data1 associated to colormap(gray)
plot2 with data2 associated to colormap(jet)
axis square
axis off
box off
set(gca,'XTick',[],'YTick',[])
set(gcf, 'units','normalized','outerposition',[0 0 1 1]) %EXPANDING FIGURE ON SCREEN
ax1 = gca; %GETTING THE AXES OF THE FORMER FIGURE
ax1p = get(ax1,'Position');
colormap(ax1,'jet') %colorbar for nodes
cb1 = colorbar('west');
set(cb1, 'Position', [ax1p(1)+ax1p(3)-0.17 ax1p(2)+0.17 0.03 ax1p(2)+ax1p(3)-.3])
set(cb1, 'fontsize', 30);
caxis([min(n_atribut) max(n_atribut)]);
ax2 = axes; %getting axes for second colorbar
set(ax2, 'Position', ax1p); % Co-locate ax2 atop ax1
ax2p = get(ax2, 'Position');
axis off; % Make ax2 invisible
linkaxes([ax1,ax2],'xy'); % Link ax1 and ax2 so zooming will work properly:
colormap(ax2,'gray') %colorbar for links
cb2 = colorbar('west'); % Create a new colorbar
set(cb2, 'Position', [ax2p(1)+ax2p(2)+0.6 ax2p(2)+0.17 0.03 ax2p(2)+ax2p(3)-.3])
set(cb2, 'fontsize', 30);
caxis([min(w_atribut) max(w_atribut)]);
axes(ax2);
hold off
Figures. The figure bellow shows the problem and it is what I get when I run my code
As you can see, even when both colorbars have their proper limits associated to data1 and data2, the second (the one to the extreme right) is plotted with colormap jet.
This next figure shows what I looking for:
Obviously, this one was edited in other software to get the colormap gray of the second colorbar.
Having said so, Can anyone could help me please? If it is important I could send the complete code and the 4 files to run and get the wrong figure!
Thanks in advance guys I appreciate all your helps.
Gohann.
You are probably looking for the freezecolor function.
Finally I got my answer due to the comment of Ratbert and the works of so-many guys from MatlabĀ® file exchange. Thankful for the scripts freezeColors.m and cbfreeze.m and double2rgb.m by John Inversen and David Legland Carlos Adrian vargas.
This how I resolved the problem.
% % % % NODOS
cm1 = colormap(jet);
cb1 = colorbar('Location', 'east', 'fontsize', 30);
caxis([min(n_atribut(n_atribut>0)) max(n_atribut)])
cbfreeze(cm1)
freezeColors(ax1)
% % % % LINKS
cm2 = colormap(bone);
cm2 = flipud(cm2);
colormap(cm2);
cb2 = colorbar('Location', 'west', 'fontsize', 30);
caxis([min(w_atribut) max(w_atribut)])
cbfreeze(cm2)
freezeColors
The function/toolKit Easy Plot EEG Brain Network is now available in GitHub and Matlab Fileexchange.
Matlab# FileExchange
GitHub Reposiroty
Thanks to everyone.

Individual line and axes styles with plotyy

I tried creating a plot with two YAxis like this:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);
Now i have two problem with this code:
I can change the Linestyles of the two hLines using hLine1.LineStyle = ':'; for example, but i can not change the styles of the two lines, that hLine1 consists of. Does anyone know how to do this?
I can't use hLine2.YLim = [0 100] to manually adjust the y-limits shown on the 2nd y-axis.
After I couldn't solve the problem using the plotyy, I searched the MATLAB documentation and found another way of implementing my plot, which I thought might be easier to handle:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right');
line(x,y3,'Parent',ax2,'Color','b')
The problem here is, that it doesn't even show the first and the second line, but only the third and i don't know why. I would prefer getting the problem solved using the plotyy, but if that's not possible I would appreciate a solution for the 2nd piece of code as well.
I think you haven't noticed that the outputs of plotyy are arrays of objects, and not single objects.
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
[hAx,hLine1,hLine2]=plotyy([x',x'],[y1',y2'],x,y3);
hLine1(1).LineStyle = '--';
hLine1(2).LineStyle = ':';
% either this
ylim( hAx(2), [0 110] );
% or alternatively
f=gcf; ylim( f.Children(2), [0 110] );
You're not seeing the first two lines because axes backgrounds are white by default. Setting the Color property of the second axes object to 'none' should give you what you're looking for:
x=linspace(0,20);
y1=linspace(10,10);
y2=x.^2;
y3=y2-y1;
figure
hold on;
line(x,y1,'Color','r')
line(x,y2,'Color','y')
ax1 = gca;
ax2 = axes('Position',ax1.Position,'YAxisLocation','right', 'Color', 'none');
line(x,y3,'Parent',ax2,'Color','b')
EDIT: I'd also recommend checking out linkaxes if you're going to be zooming/panning your axes and want to keep some or all of the axes synchronized.

Saving histograms without displaying them and adjust linewidth of plotted fit objects

I have a histogram with gaussians fitted in matlab. Here is my code:
dnbins = 100;
[counts,centers] = hist(data(:), nbins);
bar(centers, counts);
fitobject = fit(centers',counts','gauss2');hold on;
plot(fitobject,centers',counts');hold off;
How can I save the output, but without displaying it? Is it possible?
Also, I'd like to a have thicker red line, so I changed the following line:
plot(fitobject,centers',counts', 'LineWidth', 2.0);hold off;
I got the following error:
Error in color/linetype argument
Whereas this piece of code works well:
plot(centers',counts', 'LineWidth', 2.0);hold off;
Of course, it doesn't satisfy me, beacuse I want to see the fitted curve as well.
How can I change the line thickness?
Edit:
I got the following error:
This functionality is no longer supported under the -nojvm startup option.
And it makes sense, because I can't use the graphical output. I complie the code and then I run it under Linux.
How can I fix it?
First problem: you need the 'visible' property of the figure object.
...
fitobject = fit(centers',counts','gauss2'); hold on;
figure('visible','off')
plot(fitobject ,centers',counts'); hold off;
...
Second problem: using the plot function with fit-objects calls actually a different plot (cfit) function, which is part of the the Curve Fitting Toolbox. That's why the usual behavior does not apply. This little workaround however works almost always.
h = plot(fitobject, centers',counts'); hold off;
set(h, 'LineWidth',2)

Matlab and XTickLabel

I've been trying to get Matlab to change the labelling on my contourf plots for about an hour now. When I go to change XTickLabel or XTick, it simply removes my x-axis altogether! The frustrating and infuriating thing is that I'm doing exactly what all the help pages and help forums are asking me to do - I honestly don't understand why this is not working.
Hence, I am here.
My plotting code (knowledge of the function shouldn't be required - the code is rather intense. It is, however, a 2D contourf plot with valid data and ranges - the axes are the issue, not the graph):
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
axis([0 StopTime 0 0.1])
Any help would be greatly appreciated!
Solved:
I realized that the 'XTick' relied on current values of the array I was using to define the x-axis. I can't just assume matlab will evenly space a new array (at least, if there's a way to do that, I don't know). So, since I have 85,680 data points on my X-axis, I simply rescaled it by:
set(gca, 'XTick', 0:3570:85680)
set(gca, 'XTickLabel', num2cell(0:24))
Moral of the story: Matlab doesn't let you arbitrarily stick a new axis over an old one using these two functions.
You have a final axis([0 StopTime 0 0.1])) command which clears your plot, by creating a fresh new axis. That's why all your existing plots are gone. Try removing it:
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
Now the question becomes: are your ticks sensibly placed for the data you are representing? Without knowing the data I cannot answer this for you. So the ball is in your court now. ;)
You can use cell arrays to define the ticks and tick-labels and then use them with set function call, to make it more elegant -
xtick_label_cellarr = num2cell(0:24)
xtick_cellarr = linspace(0,85680,numel(xtick_label_cellarr))
set(gca, 'XTick',xtick_cellarr)
set(gca, 'XTickLabel',xtick_label_cellarr)

Matlab plot a threshold line

I want to add a horizontal threshold line to my graph of 0.7. But I can't seem to get it to work.
Code
figure(1)
plot(Variance);
hold on;
plot([1 frames], threshold, 'red')
Variance is an array to be plotted and the frames are the number of variances that are plotted. I've tried moving the hold and plots around but it doesn't seem to work either.
I know this is an easy question, but everything I've looked at online doesn't seem to work either, with regards to adding them to the same plot().
Thanks
You should change the second plot line to
plot([1 frames], [threshold threshold], 'red');