MATLAB Answers

3D Modeling MatLab Project

조회 수: 14(최근 30일)
John Hawat
John Hawat 18 Mar 2019
댓글: Rena Berman 28 Oct 2019
Hi Matlab,
I have a question reguarding my 3D modeling project I am woking on in Matlab, I choose to show the snowfall in New England specifically I found data for the years 1990-2010. The data points are below in order from 1990 to 2010. My code is also shown below. I can't seem to get the XTick's labels to display from 90 to 10 it only shows 90 to 94 or 95 and when I expand my screen it shows up to 00 but then the YTick label's repeat. Can someone please help me and fix my code, If you look at the top view of my graph I also have holes in it which do not understand.
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];
xi = 0:0.05:20;
yi = 0:0.05:5;
[xi,yi] = meshgrid(xi,yi);
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')
set(gca,'YTickLabel',{'Burlington, VT','Boston, MA','Portland Jetport, ME','North Conway, NH', 'Kingston, RI', 'Storrs,CT'})
set(gca,'XTickLabel',{'`90','`91','`92','`93','`94','`95','`96','`97','`98','`99','`00','`01','`02','`03','`04','`05','`06','`07','`08','`09','`10'})
text(0,6,86,'This is the maximum');
text(0,5, 12,'This is the minimum');
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
Burlington, Vermont 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
Boston, Massachussetts 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
Portland Jetport, Maine 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
North Conway, New Hampshire 58.9 67.7 96.3 90.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
Kingston, Rhode Island 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
Storrs, Conneticut 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

  댓글 수: 1

Rena Berman
Rena Berman 28 Oct 2019
(Answers Dev) Restored edit

Sign in to comment.

채택된 답변

Star Strider
Star Strider 18 Mar 2019
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

표시 이전 댓글 수: 1
Star Strider
Star Strider 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
John Hawat 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
Star Strider 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.

추가 답변(0개)

태그

아직 태그를 입력하지 않았습니다.


Translated by