There are an issue with Matlab app designer plots app.UIAxes - matlab

I'm creating an app in App Designer onMatlab R2017b that simulate the propagation of information in online social network. I'm displaying the results in plots app.UIAxes as shown in the figure.
Eventhough, I set all the parameters of the plot to automatic when I run the program it will set to manual again. And the following code will be generated after running the app:
% Create UIAxes7_2
app.UIAxes7_2 = uiaxes(app.Panel);
title(app.UIAxes7_2, 'Title')
xlabel(app.UIAxes7_2, 'X')
ylabel(app.UIAxes7_2, 'Y')
app.UIAxes7_2.DataAspectRatio = [1 1 1];
app.UIAxes7_2.PlotBoxAspectRatio = [1 1 1];
app.UIAxes7_2.XLim = [0 1];
app.UIAxes7_2.YLim = [0 1];
app.UIAxes7_2.ZLim = [0 1];
app.UIAxes7_2.CLim = [0 1];
app.UIAxes7_2.GridColor = [0.15 0.15 0.15];
app.UIAxes7_2.MinorGridColor = [0.1 0.1 0.1];
app.UIAxes7_2.Box = 'on';
app.UIAxes7_2.XColor = [0.15 0.15 0.15];
app.UIAxes7_2.XTick = [0 0.2 0.4 0.6 0.8 1];
app.UIAxes7_2.YColor = [0.15 0.15 0.15];
app.UIAxes7_2.YTick = [0 0.2 0.4 0.6 0.8 1];
app.UIAxes7_2.ZColor = [0.15 0.15 0.15];
app.UIAxes7_2.ZTick = [0 0.5 1];
app.UIAxes7_2.XGrid = 'on';
app.UIAxes7_2.YGrid = 'on';
app.UIAxes7_2.CameraPosition = [0.5 0.5 9.16025403784439];
app.UIAxes7_2.CameraTarget = [0.5 0.5 0.5];
app.UIAxes7_2.CameraUpVector = [0 1 0];
app.UIAxes7_2.Position = [365 233 337 202];
The way I fix this is by deleting the axis in the editor and replacing it with another. Then it runs and displays the plot on the axis as it should. but After few run the same issue come back again.The plot do not displays the hole figure but only a proportion.

Related

Error in longitude contour near the pole MATLAB

I want to plot lat/lon contours at the south pole, my data consists of grid cells, each with a lat and lon value. My problem is where the longitudes "meets" each other, as you can see in the picture, the vertical longitude in the lower part of the picture have a lot of lines on top of each other.
Does anyone know how to remove them? Here is my code:
coldiv = -57.5:2.5:-7.5;
min_pr = coldiv(1); max_pr = coldiv(end);
cmap = [0 0 0.4; 0 0 0.7; 0 0 1; 0 0.2 1; 0 0.4 1; 0 0.6 1; 0 0.8 1; 0 1 1;
0 1 0; 0.6 0.8 0; 0.6 0.6 0; 0.6 0.4 0; 0.6 0.2 0; 1 0 0];
x = linspace(1,length(coldiv)-2,length(cmap));
xi = 1:length(coldiv)-2;
cmap = interp1(x,cmap,xi);
cmap = [0. 0. 0.; cmap];
fig=figure();
set(fig, 'Position', [10 10 1500 700])
pcolor(mean(tas_ann_DMI_hist_tot,3))
shading flat
hold on;
contour(flip_lat_DMI, 'k','ShowText','on');
contour(flip_lon_DMI, 'k','ShowText','on');
caxis([min_pr max_pr]);colormap(cmap);
axesHandles = findobj(get(fig,'Children'), 'flat','Type','axes');
axis(axesHandles,'square')
set(gca,'YTick',[]);
set(gca,'XTick',[]);
I found a solution.
flip_lon_DMI(flip_lon_DMI<-160) = NaN;
flip_lon_DMI(flip_lon_DMI>190) = NaN;
...
contour(flip_lat_DMI,-90:5:-60,'k--','ShowText','on','Color',[.5 .5 .5]);
contour(flip_lon_DMI,-150:30:180,'k--','ShowText','on','Color',[.5 .5 .5]);

making a trendline in log-log scale scatter graph

I am coding to make a trendline on my scatter plot.
data=[];
for k=1:100
int=0;
for t=1:100
if k_star_90(k,t)~=0
int=int+k_star_90(k,t);
end
if k_star_90(k,t)==0 && int~=0
data=[data int];
int=0;
end
end
end
intervals = linspace(0, 1, 100);
h1 = histc(data, intervals);
scatter(intervals, h1, 'r');
set(gca,'xscale','log')
set(gca,'yscale','log')
picture of plot result
This is in log-log scale. On this plot, I want to draw y=ax+b(1st order) trendline. I am not sure how to do it.
I will really appreciate your help
I'm not sure I understood your intention correctly, but if you need a trend line you may do something like this
intervals = [0.01 0.02 0.2 0.1 0.3 0.5 0.03 0.4 0.15 0.2 0.2 0.25 1 0.9 0.8 0.8 0.7];
h1 = [70 40 4 20 2 3 60 10 50 40 10 20 1 2 3 1 2] ;
coeffs = polyfit(intervals, h1, 1);
xFitting = 0:0.01:1;
yFitted = polyval(coeffs, xFitting);
scatter(intervals, h1, 'r');
set(gca,'xscale','log');
set(gca,'yscale','log');
grid on;
hold on;
plot(xFitting, yFitted, 'b', 'LineWidth', 2);
hold off;
ylim([1 80]);
xlabel('intervals');
ylabel('h1');
Here is your trend in Log scale:
Og course it does not look like a first order trend. To depict it as a line you need to go back to a normal plot:

Is it possible to vectorize procedural operations (for/if ) in Matlab?

I wonder if the following code can be vectorized? Or, more straightforward, I am trying to match a number to several intervals, the result of which determines the update of a increment process. Thanks a lot!
pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];
for i = 1:6
if rand_num < sum(divid(1:i))
pop_matrix(i) = pop_matrix(i)+1;
break
end
end
The following should work:
pop_matrix = [10 0 0 0 0 0];
rand_num =rand;
divid = [0.05 0.05 0.1 0.2 0.1 0.1];
idx = find(cumsum(divid) > rand_num,1);
pop_matrix(idx) = pop_matrix(idx) + 1;
EDIT: A method using interp1 which is about 10x faster, assuming you want to draw N samples from the distribution called divid:
pop_matrix = [10 0 0 0 0 0];
divid = [0.05 0.05 0.1 0.2 0.1 0.1];
N = 1000; %// number of random samples to take
rand_num = rand(N,1); %// generate N random numbers
dcs = cumsum(divid); %// get cumulative distribution
dcs = dcs/dcs(end); %// ensure this is normalized to 1
dcs = [0,dcs]; %// put a zero in front to create a new bin
s = interp1(dcs, 1:length(dcs), rand_num, 'previous', NaN); %// draw samples
pop_matrix = pop_matrix + accumarray(s,1)'; %'//add up samples together
This process is basically sampling from the probability distribution defined by divid using the Inverse Transform Sampling method, where dcs is the distribution's cumulative density function (CDF).

scale part of an axis in matlab

I have the following image which I want to have the depth axis range like below :
(10 9.5 9 8.5 8 7.5 7 6 5 3 2 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0) to show the data between depth 1 and 0 in larger scale, and I have the following code
depths = [10 5 1 0.5 0; 10 5 1 0.5 0] % these are the real depths in meter
contourf(points,depths,RFU15102013_BloomAsMainPoint);
set(gca, 'XTick', points(1) : points(2), 'XTickLabel',{ 'LSB1', 'LSB2'});
ylabel('Depth(m)');
xlabel('Points');
title('Date: 15.10.2013');
this is the image :
how can I do that?
EDIT1
Real Data:
RFU15102013_BloomAsMainPoint = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
points = [1 1 1 1 1; 2 2 2 2 2 ];
depths = [10 5 1 0.5 0; 10 5 1 0.5 0];
As most of a data changes around zero it could be enough to change scaling of Y axis. Here is an example
close all; clear all;
z = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
x = repmat([1; 2], 1, 5);
y = repmat([10 5 1 0.5 0], 2, 1);
% plotting with equally spaced y-s
h = subplot(1,2,1);
contourf(x,y,z);
y2 = log(y + 0.25);
yTicks = linspace(min(y2(1,:)), max(y2(1,:)), 10);
% plotting with logarithmically spaced y-s
h = subplot(1,2,2)
contourf(x,y2,z);
set(h,'YTick', yTicks)
set(h,'YTickLabel', exp(yTicks) - 0.25);
print('-dpng','scaling.png')
The result
This way any monotonic continuous function for axis scaling can be applied.
You could use UIMAGE - UIMAGESC from the mathworks file exchange and set the y values to emphaisize points in 1 to 0 range.

Plot square surface in Matlab

How to plot a square surface in Matlab?
More exactly I want to plot a square square with value 0.5 surface which is located at X:-1 to X=1 and Y:2.5 to 3.5.
I tried the following
[X,Y] = meshgrid(-3.5:.5:3.5);
Z = zeros(15);
Z(end-2:end,5:9) = 0.5;
surf(X,Y,Z);
This doesn't result in a perpendicular edge. How to archive that?
This is what the patch function is for.
Matlab documentation
so for your case:
X = [ -1 -1 1 1];
Y = [3.5 2.5 2.5 3.5];
Z = [0.5 0.5 0.5 0.5];
patch(X,Y,Z,'red')
view(45,45)
You need to provide multiple Z-values together with the same X, Y values. A small example:
>> [X, Y]= meshgrid([1,2,2,3,4], 1:2)
X =
1 2 2 3 4
1 2 2 3 4
Y =
1 1 1 1 1
2 2 2 2 2
>> Z = [0,0,1,1,0;0,0,1,1,0]
Z =
0 0 1 1 0
0 0 1 1 0
>> surf(X, Y, Z)
Yields this:
This should be the same in 2D, you just need to wrap you head around which X and Y values to duplicate and adjust the Z-Matrix accordingly.
I ended up with
figure;
hold on;
X = [ -2 -2 2 2];
Y = [2 4 4 2];
Z = [0 0 0 0];
patch(X,Y,Z,'blue');
X = [ -1 -1 1 1];
Y = [3.5 2.5 2.5 3.5];
Z = [0.5 0.5 0.5 0.5];
h = patch(X,Y,Z,'red');
X = [ -1 -1 1 1];
Y = [2.5 2.5 2.5 2.5];
Z = [0 0.5 0.5 0];
patch(X,Y,Z,'red');
X = [1, 1, 1, 1];
Y = [2.5 2.5 3.5 3.5];
Z = [0 0.5 0.5 0];
patch(X,Y,Z,'red');
view(45,30)
legend(h, 'F(u,v)')
xlabel('u')
ylabel('v')
zlabel('F(u,v)')