1 view (last 30 days)

Show older comments

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?

the cyclist
on 4 Apr 2020

Edited: the cyclist
on 4 Apr 2020

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.

the cyclist
on 5 Apr 2020

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:

the cyclist
on 4 Apr 2020

Edited: the cyclist
on 4 Apr 2020

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
on 4 Apr 2020

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)

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

Start Hunting!