Specifc values over Axis in matlab - matlab

I want to plot 3D graph where Y-axis has only values 9, 99 , 999. I tried to use
Y= [9, 99 , 999];
set(gca,'YTickLabel',Y);
set(gca,'YTick',Y);
I want to have 3 points 9 at the beginning, 99 in the middle, and 999 at the end. Is it possible to do that?. I tried also with Ylim, but couldn't help

You can change your y-axis to a logarithmic scale.
Use:
set(gca, 'YScale', 'log');
If you also set
ylim([9 999]);
as you had pointed out, you should get the desired result.

Another solution:
F = [1 2 3];
Y = [9, 99 , 999];
Y_ = [9, (999-99)/2 , 999];
figure;
subplot(1,2,1)
plot(Y,F,'*-');
set(gca,'XTickLabel',Y,'XTick',Y);
subplot(1,2,2)
plot(Y_,F,'o-');
set(gca,'XTickLabel',Y,'XTick',Y_);
You can see the difference:

Related

Cannot plot circle with ezplot in matlab

I want to plot 6 circles in figure MATLAB. But it's cannot appear.
I have think of this code is correct, and I try to give axis limits. But it cannot fixing my problem.
clear all;
clc;
p=[8 9 3 4 7 4];
rtopi=[3 4 16 25 34 25];
n=length(p);
for ii=1:n
f=#(x,y)(x-p(ii)).^2+(y).^2-rtopi(ii)^2;
gambar=ezplot(f);
set(gambar,'color','k','linewidth',2);
grid on;
axis equal;
set(gca,'Color','y');
xlabel('Real');
ylabel('Imaginary');
title('Discs');
axis([-30 30 -30 30]);
end
This is the result :
How to fix it?
Remove the .* in the function definition, just use x^2 not x.^2.
Use 'hold on' before end of loop
Move the background color, grid on, title etc. outside the loop.
And most importantly, declare xmin, xmax for the ezplot. Default is (-2pi to 2pi).
Try: gambar = ezplot(f,[xmin,xmax}) and use the plot limits for the xmin and xmax

How to plot a multidimensional array in matlab?

I have a table as follows
system:index 2017_06_18 2017_06_19 2017_06_20 2017_06_21
2 612.8099664 1174.656713 1282.083251 815.3828357
3 766.4103726 1345.135952 1322.726083 749.998993
4 765.0230453 1411.669136 1350.437586 610.9541838
5 553.5858458 1374.14789 1152.086957 566.7924468
6 466.9780908 1311.903756 1060.494001 559.1982264
7 257.1162602 1270.182385 988.5455285 562.9224932
8 230.6611542 1310.971988 1001.548768 502.3266959
I want to plot a 2d-colormap representing system:index as y axis, dates as x axis and values under dates as colors. I tried with the following code but it did not give what I want.
clear
clc
filename = 'TurbidityDailyMean.xlsx';
data = xlsread(filename,'TurbidityDailyMean','A1:E8');
figure;
hold on
for i = 2:5
y = data(:,1);
x = data(:,i);
plot(x,y)
end
I need to map a colormap as mentioned above. But from what I tried it gives something else. And another fact is that I can't insert system:index and dates row into matlab with relevant data.
Thanks to my supervisor Dr.Kavinda,enter image description here the solution I adopted from him was as follows
data1 = csvread('TurbidityDailyMean.csv',1,1);
[mm,nn]=size(data1)
xx=ones(mm,nn);
yy=ones(mm,nn);
for j=1:nn
xx(:,j)=j;
end
for i=1:mm
yy(i,:)=i;
end
zz=data1;
h1=pcolor(xx,yy,zz);
shading flat
set(gcf,'color',[1,1,1])
axis([1 5 1 7]);
jet2=jet;
jet2(1,:)=1.0;
colormap(jet2)
caxis([0.0 0.13])
hold on
xlabel('Julian Day');
ylabel('y (?~1 km)');
colorb=colorbar;
set(get(colorb,'ylabel'),'String','Normalized Red Band Reflectance','fontsize',15,
'color', 'k');
set(get(colorb,'xlabel'),'fontsize',20, 'color', 'k');
grid on

Matlab plotting a value which is referenced by another 2 values

In every iteration I get these values. For instance;
a is 2, b is 3, gg(a,b) is 70
a is 2, b is 4, gg(a,b) is 72
a is 2, b is 5, gg(a,b) is 76
I want to plot these in a one plot like 'a' in the x axes, 'b' in the y axes and gg(a,b) is the value which is referenced by a and b. I also want to show gg(a,b) values in a colormap. I tried but not achieved yet. Could you help please?
Here is what I've tried. I don't want 3d but don't know how to plot. Lets say gg is a matrix includes 20 columns and 5 rows.
gg=rand(5,20);
for a=1:5
for b=1:20
hold on
scatter3(gg(a,b),a,b)
xlabel('gg(a,b)'), ylabel('a'), zlabel('b')
colormap(jet)
view(3)
end
end
Here is a (simpler) code for both:
gg = rand(5,20);
[a,b] = ndgrid(1:5,1:20);
figure
scatter(a(:),b(:),[],gg(:))
colormap(jet)
xlim([0 6])
xlabel('a')
ylabel('b')
colorbar
figure
colormap(jet)
imagesc(1:5,1:20,gg.')
xlabel('a')
ylabel('b')
axis xy
colorbar
which creates:
I think it should be gg=rand(5,20);

MATLAB: add a line to 3D plot

I would like to add a reference line on a 3d plot which follows the surface built with mesh(). I have 3 points in the x and y axis to build the line and so will need to interpolate to find z axis coordinates. Here is my code (with reproducible data) so far:
acceleration_dB = [0 109.3699 118.0084 133.9584 104.3017 110.5423 120.6332 140.6567 144.4194 129.7292]';
frequency = [1 50 50 50 100 100 100 100 100 500]';
voltage = [ 1.0e-04 * 0.0001 0.0968 0.1645 0.2983 0.0278 0.0368 0.0893 0.2785 0.4928 0.0780 ]';
xlin=linspace(0, max(frequency),33);
ylin=linspace(min(acceleration_dB), max(acceleration_dB),33);
[X, Y] = meshgrid(xlin,ylin);
f = scatteredInterpolant(frequency,acceleration_dB,log(voltage));
Z=f(X,Y);
figure();
mesh(X,Y,Z);
hold on
% Add threshold reference line
threshAccel = [97.0870 104.4212 109.5787]
threshFreq = [50 100 500]
Zthresh=f(threshFreq,threshAccel);
plot3(threshFreq,threshAccel,Zthresh,'Color','black','LineWidth',1)
Which gives:
What I would like is the black line following the plane of the surface for the whole length of the x-axis.
Any advice would be greatly appreciated. Thank you!
It is just a problem of range I think. This seems to work pretty well:
threshAccel = [97 97.0870 104.4212 109.5787]
threshFreq = [0 50 100 500]
but I am not sure about that 97 and 0 are the precise values. You should correct them probably.

Matlab how to add values in the x-axis of a plot

Plot using `set(gca, 'XTick', [1 10 20 50 100])
Hi everyone!
I have created a graph with the function scatter and in the x-axis there are only three values shown: [1 10 100].
I'd like to add some values, specifically I'd like to show [1 5 10 20 50 100].
How can i do this?
My code is:
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'XTick',[1 10 20 50 100])
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
You want to set your XTick values before you set your XTickLabels since you are constructing your XTickLabels from the values of the XTicks themselves.
What is currently happening is that you have 5 XTick values and only 3 labels. Because of this, MATLAB will repeat the labels that you have to populate labels for all XTick locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca,'XScale','log')
set(gca,'XTick',[1 10 20 50 100])
set(gca,'XTickLabel',num2str(get(gca,'XTick').'))
set(gca,'YScale','log')
set(gca,'YTickLabel',num2str(get(gca,'YTick').'))
grid on
Better yet, there is no real reason for you to be setting XTickLabel manually here. If you change the XTick locations, the labels will automatically be updated to reflect the new locations.
line(contrast2*100, RNorm2,'color','black');
hold on
scatter (contrast2*100, RNorm2,'y','filled');
set(gca, 'XScale', 'log', ...
'XTick', [1 10 20 50 100], ...
'YScale', 'log')