Create an array of 'Geostruct' Data in Matlab - matlab

Right now I am creating mapstructs in Matlab and then exporting them individually as shape files using the shapewrite() function.
However, instead of exporting them individually I want to store all of them into an array and then save it at the end as one single shapefile which holds all of the points from the mapstructs stored in the array.
My problem is I don't know how to initialize an array to hold these mapstructs. I've tried
`a = struct(sizeofarray)`
but it isn't compatible with mapstructs. I would appreciate any help!

You can store any kind of data in a cell array:
a = cell(sizeofarray,1);
You can then assign them like this:
a{1} = firstmapstruct;
a{2} = secondmapstruct;
However, if I understand you correctly you have mapstructs from the MATLAB Mapping Toolbox and want to concat structs of this form:
firstmapstruct =
609x1 struct array with fields:
Geometry
BoundingBox
X
Y
STREETNAME
RT_NUMBER
CLASS
ADMIN_TYPE
LENGTH
So you should probably do
a = firstmapstruct;
a(end+1:end+numel(secondmapstruct))= secondmapstruct;
and so on...

If all of your individual mapstructs have the same fields, you should be able to initialize a structure array by replicating one of your mapstructs using the function REPMAT:
a = repmat(mapstruct1,1,N); %# A 1-by-N structure array
Then just fill in each element as needed:
a(2) = mapstruct2; %# Assign another mapstruct to the second array element
a(3).X = ...; %# Assign a value to the X field of the third element
a(3).Y = ...; %# Assign a value to the Y field of the third element
You can find out more information about Geographic Data Structures in this documentation.

Related

How to gather results with parfor in MATLAB

I have a function called SpreadFinder which returns an array of structures:
[ spreads ] = SpreadFinder( pp, corr_thres, i_range )
The return object looks like this:
spreads =
1x4026 struct array with fields:
px
tuple
beta
The code is:
m = containers.Map(0, 0, 'uniformValues',false); m.remove(0);
parfor col = 1:8
spreads = SpreadFinder(pp, 0.8, i_ranges(:,col)');
m(col) = spreads;
end
When I run it, I have this error:
Subscripted assignment dimension mismatch.
I know the Map can be used to store an array of structures. (https://stackoverflow.com/a/2365526)
This code below works. So I don't think it's a problem of dimension...
m(1) = spreads(1:2);
m(2) = spreads(1:4);
The objective is to merge all the results into one map and possibly iterate over this map after the parallel task.
Can someone help me out?
Best,
IIRC, you cannot assign to a containers.Map inside a parfor because a containers.Map is not sliceable. If you want to assign elements to an array inside a parfor, it must be sliceable. What I mean by sliceable is that you are able to extract a subset of the values via the colon operator (i.e. 1:3, 2:4), etc. You cannot do that with a containers.Map unless you specifically use the values function, and from there you can specify a cell array of keys and get the values from there. Using () syntax only permits you to access one key at a time.
Therefore, what I would suggest you do is assign the outputs to a cell array, and use this cell array as values into your containers.Map. Something like this:
%// Create temporary cell array of values
c = cell(1,8);
%// Run parfor for each col
parfor col = 1:8
c{col} = SpreadFinder(pp, 0.8, i_ranges(:,col)');
end
%// Make a new containers.Map for these values
m = containers.Map(1:8, c, 'uniformvalues', false);
%clear c; %// Optional if you don't want the temporary cell array to stick around

Dividing a DataArray and putting names to each Matlab

I have a data array with dimensions 1 x 95125. I want to extract data from it and then give each a name. For example
Station00001=[R{:,1:13}]
Station00002=[R{:,15:27}]
.....
Station06518
The question is, is it possible to create a vector with all the wanted names and then open each data from the data array as above but with all the files with a for loop to its corresponding file
This is what I did but its not working
for i= 1:(length(R)/14)
k=0:((length(R)/14)-1)
A(i)=1+14.*k;
B(i)=A+12;
Stations (i)= [R{:,A(i):B(i)}];
end
Your loop is fine; not sure why you would want to store the A and B indices in an array, though.
numStations = floor(length(R)/14); %# careful: the number of columns in R is not a multiple of 14
Stations = cell(1,numStations);
for i= 1:numStations
fromColumn = (i-1)*14+1;
toColumn = i*14-1;
Stations{i}= [R{:,fromColumn:toColumn}];
end
To access the data from Station 25, use Stations{25}

Matlab - dynamical adding data in structure

Matlab’s Image Processing Toolbox contains the function bwconncomp which gives a Matlab structure containing (among others) the number of objects and a list of the pixels in each component.
I would like to produce a similar output for the intersections of a skeleton [found by bwmorph(matrix,'branchpoints')]. However, I am unfamiliar with how to declare an open structure.
My intent is to search through the matrix and adding information regarding each as I go along. Since the some of the point given by 'branchpoints' can represent the same intersection I do not know the number of intersections, or the number of pixels needed to store in my pixel id list.
How can I keep adding pixels into the cell containing all pixels for a given intersection (CC.PixelIdcList in the output from bwconncomp)
Can anyone help?
I'm not certain exactly what you're asking but here are two ways of adding fields to a struct:
Firstly we can just add fields by putting values in them. These fields can be arrays or cell arrays and you can grow them dynamically just like any matlab array:
s.p{1} = 5;
s.p{2} = 10;
s.p{3} = 'I''m a string!';
Secondly, if you really have to, you can dynamically create field names using strings:
for n = 1:3
name = ['p', num2str(n)];
s.(name) = n/10;
end
This results in:
disp(s)
scalar structure containing the fields:
p =
{
[1,1] = 5
[1,2] = 10
[1,3] = I'm a string!
}
p1 = 0.10000
p2 = 0.20000
p3 = 0.30000
Use bwconncomp on the matrix from bwmorph(matrix,'branchpoints').

How to get all data from a struct?

I've got a result from a web service and MatLab gladly notifies that it is a 1x1 struct. However, when I try to display it (by typing receivedData and pressing enter) I get to see this:
ResponseData: [5x1 struct]
Equally gladly, I entered the following, in my attempt to get the data viewed to me:
struct2array(responseData)
I get only a bunch (not five, more than five) of strings that might be names headers of columns of the data provided.
How do I get all the data from such structure?
You may use fieldnames to get the data from the struct and then iteratively display the data using getfield function. For example:
structvariable = struct('a',123,'b',456,'c',789);
dataout = zeros(1,length(structvariable)) % Preallocating data for structure
field = fieldnames(a);
for i = 1:length(structvariable)
getfield(structvariable,field{i})
end
Remember, getfield gives data in form of cell, not matrix.
or you can use cellfun function:
% STRUCT - your structure
% tmp_nam - structure field names
% tmp_val - values for structure field names
tmp_nam = fieldnames(STRUCT);
tmp_val = cellfun(#(x)(STRUCT.(x)),tmp_nam,'UniformOutput',false);
In order to display all elements and subelements of a structure there is a custom function that you can download here:
http://www.mathworks.com/matlabcentral/fileexchange/13831-structure-display
Some useful access syntax:
someVar = ResponseData(1) %Displays the first element
someVar = ResponseData(4) %Displays the 4th element
To display them all, one after the next
for ix = 1:length(ResponseData)
tmp = ResponseData(ix);
end
To get all the fieldnames
names = fieldnames(ResponseData)
To get all 5 data elements from the structure with the first field names, and put them into a cell array
ixFieldName = 1;
someCellArray = { ResponseData.(ixFieldName{1}) }
A small correction to Sahinsu's answer:
structvariable = struct('a',123,'b',456,'c',789);
dataout = zeros(1,length(structvariable)) % Preallocating data for structure
field = fieldnames(structvariable);
for i = 1:length(field)
getfield(structvariable,field{i})
end

Extracting a matrix of data from a matrix of structs

I have a matrix of structs. I'm trying to extract from that matrix a matrix the same size
with only one of the fields as values.
I've been trying to use struct2cell and similar functions without success.
How can this be done?
If I understand you correctly, you have an array of struct like e.g this
s(1:2,1:3) = struct('a',1,'b',2);
Now you want a different struct that only has the field b
[newS(1:2,1:3).b] = deal(s.b);
edit
If all you need is the output (and if the field values are scalar), you can do the following:
out = zeros(size(s));
out(:) = cat(1,s.b)
I'll borrow Jonas' example. You can use the [] to gather a particular field.
% Create structure array
s(1:2,1:3) = struct('a',1,'b',2);
% Change values
for idx = 1:prod(size(s))
s(idx).a = idx;
s(idx).b = idx^2;
end
% Gather a specific field and reshape it to the size of the original matrix
A = reshape([s.a],size(s));
B = reshape([s.b],size(s));
I have a similar problem, but the contents of the field in my structure array are varying length strings that I use to tag my data, so when I extract the contents of the field, I want a cell of varying length strings.
This code using getfield and arrayfun does the job, but I think it is more complicated than it needs to be.
sa = struct('name', {'ben' 'frank', 'betty', 'cybil', 'jack'}, 'value', {1 1 2 3 5})
names = arrayfun(#(x) getfield(x, 'name'), sa, 'UniformOutput', false)
Can anyone suggest cleaner alternative? extractfield in the mapping toolbox seems to do the job, but it is not part of the base MATLAB system.
Update: I have answered my own embedded question.
names = {sa.name}