I keep on getting this error when I try to turn my given data into a graph

1 次查看(过去 30 天)
I am really new at MATLAB but i have to turn my 3 csv files into a graph. This is my code that I cuurently have
A=importdata('mariana_depth.csv');
lon=importdata('mariana_longitude.csv');
lat=importdata('mariana_latitude.csv');
surfc(A,lon,lat);
colorbar;
And this is the error i get
Error using surfc (line 46)
The surface Z must contain more than one
row or column.
Error in Project_2_1 (line 4)
surfc(A,lon,lat);
How would I code this correctly?
  2 个评论
the cyclist
the cyclist 2020-4-4
编辑:the cyclist 2020-4-4
Can you upload your data in a MAT file (using the paper clip icon)? If not, can you at least report the values of
size(A)
size(lon)
size(lat)
Michelle
Michelle 2020-4-4
the files are too large but the values are
A
1320x1440 matrix
lon
1440x1 matrix
lat
1320x1 matrix

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2020-4-4
编辑:the cyclist 2020-4-4
A=importdata('mariana_depth.csv');
lon=importdata('mariana_longitude.csv');
lat=importdata('mariana_latitude.csv');
[lat_grid,lon_grid] = meshgrid(lat,lon);
figure
surfc(lat_grid,lon_grid,A);
colorbar;
Note that because of the large amount of data you have, the figure quality is not that great. (If the figure window is not very large, it appears as just a blackish blob.) The contour plot renders pretty well, though.
You could consider just using a sample of the data. For example
figure
surfc(lat_grid(1:5:end,1:5:end),lon_grid(1:5:end,1:5:end),A(1:5:end,1:5:end));
colorbar;
looks much better:
Or you could plot in sections.
  2 个评论
Walter Roberson
Walter Roberson 2020-4-4
I always recommend using 'edgecolor', 'none' when drawing surface plots. If you use shading then it automatically turns off the edge color.
the cyclist
the cyclist 2020-4-5
编辑:the cyclist 2020-4-5
That's a good tip. Here's OP's plot with full data.
Depending on your purpose here, you might want to use the contour plot as well, which makes some of the structure clearer:

请先登录,再进行评论。

更多回答(2 个)

the cyclist
the cyclist 2020-4-4
编辑:the cyclist 2020-4-4
I can guess at a couple problems here, and I'm also going to guess that you didn't really study the documentation for surfc, to understand the inputs.
First, I expect you want A to be the third input, since it is the depth (corresponding to the Z input in the documentation).
Second, I'm guessing that your variables are just three vectors. As can be seen in the examples in the documentation, you need to form the inputs into a grid. The meshgrid command is handy for that.
I'll leave it at that, and wait on your reply (and perhaps the data).

Walter Roberson
Walter Roberson 2020-4-4
In some cases, the data forms an implicit grid.
For example if your longitude repeated every 5 values and your latitude had 5 copies of every value in a row, then that would be data for a implicit grid that was 5 by something.
In such cases, when you identify the repeat period, you can reshape() the lat, lon, and A values according to the repeat period, in order to get a 2D array. In some cases you might need to reshape() with the period in the other position than you expect and transpose the result. For example,
reshape(lon, 5, [])
versus
reshape(lon, [], 5).'
However, in other cases, there is no implicit grid. There might be an approximation of a grid, such as you might get if you did not bother to put in any entries for locations that were on land, but using reshape() only works if there is a full implicit grid. In such cases, use scatteredInterpolant() and invoke it on a grid of locations to sample at:
F = scatteredInterpolant(lon, lat, A);
N = 100;
lonv = linspace(min(lon), max(lon), N);
latv = linspace(min(lat), max(lat), N);
[lonG, latG] = ndgrid(lonv, latv);
Ainterp = F(lonG, latG);
surf(lonG, latG, Ainterp)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by