MATLAB Answers

0

3D Modeling MatLab Project

Asked by John Hawat on 18 Mar 2019
Latest activity Edited by John Hawat on 20 Aug 2019
This question was flagged by Star Strider

  0 Comments

Sign in to comment.

Tags

No tags entered yet.

1 Answer

Answer by Star Strider
on 18 Mar 2019
 Accepted Answer

You need to define the number of your 'XTick' values to be the same as the number of labels you want to display.
Try these lines to define your 'XTick' values and their labels:
xt = get(gca, 'XTick');
xtklbl = {'`90','`91','`92','`93','`94','`95','`96','`97','`98','`99','`00','`01','`02','`03','`04','`05','`06','`07','`08','`09','`10'};
xtix = linspace(min(xt), max(xt), numel(xtklbl));
set(gca, 'XTick',xtix, 'XTickLabel',xtklbl)
Those are in place of the one set call you had originally.

  4 Comments

Show 1 older comment
Star Strider
on 19 Mar 2019
I would do the same thing for the 'YTick' values:
yt = get(gca, 'YTick');
ytklbl = {'Burlington, VT','Boston, MA','Portland Jetport, ME','North Conway, NH', 'Kingston, RI', 'Storrs,CT'};
ytix = linspace(min(yt), max(yt), numel(ytklbl));
set(gca,'YTick',ytix, 'YTickLabel',ytklbl)
In order to put the maximum and minimum values correctly, I had to alter your code slightly so you would not overwrite ‘xi’ and ‘yi’ (the original versions of which I renamed ‘xv’ and ‘yv’).
The new version:
x = 0:20;
y = 0:5;
z = [42.5 79.2 116.8 107.2 60.5 100 95 93.9 70.7 76.4 122.4 56.2 83 94.7 89.7 70.4 94.6 120.2 91.4 96.5 98.5; 19.1 22 83.9 96.3 14.9 107.6 51.9 25.6 36.4 22.4 44.2 15.1 67.7 35.1 86.6 39.9 17.1 51.2 65.9 35.7 79; 32.4 58.6 115.6 76.2 38.6 102 43.3 54.5 53.5 41.1 99.3 32.6 63.9 46.7 102.1 47.2 54.9 103 81.1 37.1 69.3; 58.9 67.7 96.3 80.2 63.4 114.7 103.5 85.5 80.6 72.4 106.6 64.1 77.2 69 76 47.9 81.7 147 109.6 52.6 66.1; 18 18.6 33.3 55.8 11.7 75.7 28.1 7.4 39.2 16.9 37.5 9.1 49.9 41.6 65.2 28.4 13.6 14.1 47.8 37.5 64.4; 59.5 43.7 106.3 98.1 40.9 153.2 100.1 59.7 47.3 46.7 93.9 30.3 88.5 62 90.8 80.3 44 79.8 78.9 73.3 89.9];
xv = 0:0.05:20;
yv = 0:0.05:5;
[xi,yi] = meshgrid(xv,yv);
zc = interp2(x,y,z,xi,yi,'cubic');
mesh(xi,yi,zc);
% surfc(xi,yi,zc);
xlabel('Years')
ylabel('Cities')
zlabel('Inches of Snow')
title('Snowfall in New England 1990-2010')
yt = get(gca, 'YTick');
ytklbl = {'Burlington, VT','Boston, MA','Portland Jetport, ME','North Conway, NH', 'Kingston, RI', 'Storrs,CT'};
ytix = linspace(min(yt), max(yt), numel(ytklbl));
set(gca,'YTick',ytix, 'YTickLabel',ytklbl)
xt = get(gca, 'XTick');
xtklbl = {'`90','`91','`92','`93','`94','`95','`96','`97','`98','`99','`00','`01','`02','`03','`04','`05','`06','`07','`08','`09','`10'};
xtix = linspace(min(xt), max(xt), numel(xtklbl));
set(gca, 'XTick',xtix, 'XTickLabel',xtklbl)
[minsnw,idxmin] = min(zc(:));
[maxsnw,idxmax] = max(zc(:));
[rmin,cmin] = ind2sub(size(zc),idxmin);
[rmax,cmax] = ind2sub(size(zc),idxmax);
text(xv(cmax),yv(rmax),maxsnw,{'This is the maximum';'\downarrow'}, 'HorizontalAlignment','center', 'VerticalAlignment','bottom');
text(xv(cmin),yv(rmin),minsnw,{'\uparrow';'This is the minimum'}, 'HorizontalAlignment','center', 'VerticalAlignment','top');
view(-50, 20)
Experiment to get the result you want.
John Hawat on 19 Mar 2019
Thank you so much! You are a life saver! That code worked perfectly;however, I'm wondering why you removed the surfc(xi,yi,zc); command from my code and if it would stil work fine if I added it. I'm also wondering if you could please make comments on the sections of code that you added explaining what they do and how they work so I can know and uncerstand how to use them for next time if possible please help me. I am forever greatful
Star Strider
on 19 Mar 2019
As always, my pleasure!
The 'YTick' change works the same as the 'XTick' change, because to define tick label positions correctly, the number of ticks must match the number of tick labels. The linspace function creates a vector from the minimum value to the maximum value, specifying the number of elements in the vector it returns, and so the number of ticks to plot.
The minimum and maximum snow values use the min and max functions to get the minimum and maximum and the corresponding linear indices into ‘zc’. The ind2sub function converts the linear indices to row and column references (to ‘zc’ here). Those are then used to create the appropriate ‘xv’ and ‘yv’ values to place the annotations correctly with the text calls. (I could also have use the find function to get the row and column positions using the maximum and minimum values, however it seemed easier and more efficient to use ind2sub with the linear indices, since they were already returned by the min and max functions.) The arrows and relative positions of the strings are defined by the other arguments to the text function.

Sign in to comment.