Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
add entries into structarray

Subject: add entries into structarray

From: patrick

Date: 15 Apr, 2011 07:40:05

Message: 1 of 5

Dear all,

I just want to modify entries in a structarray (i.e. insert Lat and Lon field that I can process the data with the Mapping Toolbox):

1. It would be good to rename the field "X" ("Y") and replace it by "Lon" ("Lat"). I found no solution to that. Instead I used

2. Instead I used the command "setfield". Problem: Whenever I use this command (see below) to change one field, the dimensions of my structarray changes. Here: the structarray hydrostations has (2,1) dimesions. After execution of "hydrostations(1,1) = setfield('Lat',15.77)" the dimension changes to (1,1).

Again the commands:

1. load shape file into workspace > structarray
hydrostations=shaperead('Hydro_station.shp')

2. modify entires in structarray
hydrostations = setfield(hydrostations(1,1),'Lat',15.77)

Results: dim of hydrostations is reduced to (1,1)-structarray, before step 2 it is a (2,1)-structarray

Any help is highly appreciated.

Pat

Subject: add entries into structarray

From: Hazel

Date: 15 Apr, 2011 08:39:04

Message: 2 of 5

"Patrick" wrote in message <io8sol$9g0$1@fred.mathworks.com>...
> Dear all,
>
> I just want to modify entries in a structarray (i.e. insert Lat and Lon field that I can process the data with the Mapping Toolbox):
>
> 1. It would be good to rename the field "X" ("Y") and replace it by "Lon" ("Lat"). I found no solution to that. Instead I used
>
> 2. Instead I used the command "setfield". Problem: Whenever I use this command (see below) to change one field, the dimensions of my structarray changes. Here: the structarray hydrostations has (2,1) dimesions. After execution of "hydrostations(1,1) = setfield('Lat',15.77)" the dimension changes to (1,1).
>
> Again the commands:
>
> 1. load shape file into workspace > structarray
> hydrostations=shaperead('Hydro_station.shp')
>
> 2. modify entires in structarray
> hydrostations = setfield(hydrostations(1,1),'Lat',15.77)
>
> Results: dim of hydrostations is reduced to (1,1)-structarray, before step 2 it is a (2,1)-structarray
>
> Any help is highly appreciated.
>
> Pat

It's a little difficult to know what's going on without having an example of what comes out of:
    shaperead('Hydro_station.shp')

But to start with ....
1) hydrostations(1,1) = setfield('Lat',15.77)
is NOT what is being said by
2) hydrostations = setfield(hydrostations(1,1),'Lat',15.77)

I'm assuming by (1) you meant that hydrostations(1,1) is to be updated?
In (2), you are adding the field 'Lat' to the structure in hydrostations(1,1) then overwriting the whole hydrostations with this new structure.

I think what you want is
    hydrostations(1,1) = setfield(hydrostations(1,1),'Lat',15.77)

Subject: add entries into structarray

From: Rob Comer

Date: 16 Apr, 2011 00:35:08

Message: 3 of 5

Hazel's suggestion about using setfield should work, but you can also reference the fields directly. Here's an example (using a shapefile that ships with Mapping Toolbox) that shows how to replace field X with Lon and field Y with Lat, for all elements of your structure array:

S = shaperead('worldrivers.shp')
[S.Lat] = deal(S.Y)
[S.Lon] = deal(S.X)
S = rmfield(S,{'X','Y'})

But this isn't necessary at all if you use the 'UseGeoCoords' parameter when calling shaperead:

S = shaperead('worldrivers.shp','UseGeoCoords',true)

Rob Comer
Mapping Toolbox Development
MathWorks

Subject: add entries into structarray

From: Erica B

Date: 9 Sep, 2011 18:28:14

Message: 4 of 5

> S = shaperead('worldrivers.shp')
> [S.Lat] = deal(S.Y)
> [S.Lon] = deal(S.X)
> S = rmfield(S,{'X','Y'})
>
> But this isn't necessary at all if you use the 'UseGeoCoords' parameter when calling shaperead:
>
> S = shaperead('worldrivers.shp','UseGeoCoords',true)
>
> Rob Comer
> Mapping Toolbox Development
> MathWorks

Thanks for the sample code. I tried using shaperead with 'UseGeoCoords', but I was getting very strange values for my Lats and Lons (values greater than 500, for example). Any idea what caused this? I made a workaround by creating lat lon fields in GIS, and using 'calculate geometry' to get lat lons in decimal degrees.

Now I have a related problem of not being able to plot the geodata on top of something else, or even as a subplot.

I'm attempting to plot roadway networks (data here http://www.ops.fhwa.dot.gov/freight/freight_analysis/faf/faf3/netwkdbflow/index.htm) on top of model data. I can read in the shapefile, and after I've manually calculated lat and lon, I am able to use geoshow to plot the polylines. However, when I try to plot them on top of another map - same map axes, lat-lon boundaries - nothing happens. No errors, just nothing. Even when I try to plot the shapefile as a subplot next to the model data, I get nothing.

I'm only able to plot the shapefile in it's own figure window. Any idea why this is?

Also, is it possible to adjust the linewidth in geoshow? I could with mapshow using
mapshow(faf,'LineWidth',2);
However the same syntax doesn't work with geoshow.
also g=geoshow(faf);
set(get(g,'Children'),'LineWidth',2);
didn't work.

Any guidance is much appreciated.

Thanks,
EB

PS - using MATLAB 2010b for OSX (snow leopard)

Subject: add entries into structarray

From: Rob Comer

Date: 13 Sep, 2011 00:16:29

Message: 5 of 5

"Erica B" wrote in message <j4dlru$3vg$1@newscl01ah.mathworks.com>...
> > S = shaperead('worldrivers.shp')
> > [S.Lat] = deal(S.Y)
> > [S.Lon] = deal(S.X)
> > S = rmfield(S,{'X','Y'})
> >
> > But this isn't necessary at all if you use the 'UseGeoCoords' parameter when calling shaperead:
> >
> > S = shaperead('worldrivers.shp','UseGeoCoords',true)
> >
> > Rob Comer
> > Mapping Toolbox Development
> > MathWorks
>
> Thanks for the sample code. I tried using shaperead with 'UseGeoCoords', but I was getting very strange values for my Lats and Lons (values greater than 500, for example). Any idea what caused this? I made a workaround by creating lat lon fields in GIS, and using 'calculate geometry' to get lat lons in decimal degrees.

It must be that the coordinates in your shapefile are not latitudes and longitudes. I had the impression that they were, and that you only wished to change the field names of your structure, but your mention of 'calculate geometry' makes it seem that this is not the case. So my suggestion about 'UseGeoCoords' does not apply here. The 'UseGeoCoords' option changes the field names, placing the X-values from the shapefile in the Lon field and the Y-values in the Lat field, but it does not do any calculations and does not change any numerical values.

> Now I have a related problem of not being able to plot the geodata on top of something else, or even as a subplot.

This is to be expected, if values that are not actually latitudes and longitudes are interpreted as such. It looks like you'll have to update the values after importing them -- or perhaps convert to latitude-longitude in your GIS and export a shapefile that actually is in lat-lon.

> I'm attempting to plot roadway networks (data here http://www.ops.fhwa.dot.gov/freight/freight_analysis/faf/faf3/netwkdbflow/index.htm) on top of model data. I can read in the shapefile, and after I've manually calculated lat and lon, I am able to use geoshow to plot the polylines. However, when I try to plot them on top of another map - same map axes, lat-lon boundaries - nothing happens. No errors, just nothing. Even when I try to plot the shapefile as a subplot next to the model data, I get nothing.

This could be due to the same data issue. Due to misinterpretation of coordinates and/or units, all features could easily fall well outside the limits of the map and thus get trimmed away completely.

> I'm only able to plot the shapefile in it's own figure window. Any idea why this is?

That could be because its coordinate are scaled very differently from everything else.

> Also, is it possible to adjust the linewidth in geoshow? I could with mapshow using
> mapshow(faf,'LineWidth',2);
> However the same syntax doesn't work with geoshow.
> also g=geoshow(faf);
> set(get(g,'Children'),'LineWidth',2);
> didn't work.

I'm not sure what faf is, but LineWidth can be supplied when plotting lines with geoshow, like this, for example:

>> coast = load('coast.mat');
>> worldmap world
>> geoshow(coast.lat, coast.long, 'LineWidth', 2)



> PS - using MATLAB 2010b for OSX (snow leopard)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us