read coordinate text file for regionprop usage - Matlab - matlab

I have a coordinate text file and I wish to read it into a regionprop. I wish to use the regionprop for few analysis like Centroid. How can I do it?
Code:
filename = fullfile('E:/outline.txt');
fileID = fopen(filename);
C = textscan(fileID,'%d %d');
fclose(fileID);
stats = regionprops(C,'Centroid')
coordinate text file content is as follow:
88 10
87 11
87 12
88 13
88 14
92 21
93 22
93 23
94 24
95 25
100 33
101 34
102 34
103 34
103 33
103 32
103 31
103 30
103 29
103 28
103 27
102 26
102 25
101 24
101 23
100 22
100 21
100 20
99 19
99 18
94 12
93 12
92 12
91 11
90 11
89 10
88 10

Why don't you just use centroid, which was introduced in 2017b?
[x,y] = centroid(C);
If you are insistent on regionprops (which is slower, and less accurate than operating on the polygon directly) then you are misunderstanding how region props works. Region props works on images. You need to first create an image, then pass the image to region props.
bw = roipoly(zeros(120), C(:,1), C(:,2));
stats = regionprops(bw);

You can find roipoly useful: this allows you to convert a list of 2d points/polygon vertices into a binary mask.
The resulting binary mask can then be fed to regionprops

Related

plot coordinate text file for regionprop usage - Matlab

Based on a previous question (read coordinate text file for regionprop usage - Matlab) I wish to plot the object with an extended boundaries (zeros+10) but it does not work. Any reason why?
Code:
clc;
clear;
filename = fullfile('E:/outline.txt');
fileID = fopen(filename);
C = textscan(fileID,'%d %d');
fclose(fileID);
xMax = double(max(C{1})-10)
yMax = double(max(C{2})+10)
bw=roipoly(zeros(xMax ,yMax ),C{1},C{2});
imshow(bw);
stats = regionprops(bw);
coordinate text file content is as follow:
88 10
87 11
87 12
88 13
88 14
92 21
93 22
93 23
94 24
95 25
100 33
101 34
102 34
103 34
103 33
103 32
103 31
103 30
103 29
103 28
103 27
102 26
102 25
101 24
101 23
100 22
100 21
100 20
99 19
99 18
94 12
93 12
92 12
91 11
90 11
xMax = double(max(C{1})-10)
That should probably be +10. This way you make the image smaller than your polygon.
If you want to extend the image on the left side also, add an offset to your polygon coordinates:
bw = roipoly(zeros(yMax, xMax), C{1}+5, C{2}+5);
Note also that I swapped xMax and yMax from your code, this might be another issue you're seeing. Matrix dimensions are specified as (height, width), as are indices into the matrix. But some functions such as roipoly take coordinates with x first and y second. This is a common pitfall with MATLAB syntax.
I just learned that the above is the same as
bw = roipoly(yMax, xMax, C{1}+5, C{2}+5);

Plot 2D topographic map of EEG node network on MATLAB

I would like to plot a topographic map from EEG network. The electrodes (nodes) have a associated networks metric and from these values I want to interpolate between them and plot in a head shape. Here is the code that i have reseached and the result that I am getting...
=========================================
%The position X and Y as integers (electrodes position) and the value of Z (network metric)
X = [36 51 66 11 22 51 79 91 3 16 51 86 99 1 14 51 88 101 3 16 51 86 99 11 22 51 79 91 36 51 66];
Y = [99 101 99 80 85 87 85 80 66 69 70 69 66 51 51 51 51 51 36 33 32 33 36 22 17 15 17 22 3 1 3];
Z = [-404 -566 -379 -71 -102 -119 -87 9 -62 -160 -104 -81 -26 12 -120 -176 -85 -13 0 -118 -288 -159 -36 -115 -145 -292 -215 -266 -235 -364 -192];
%Making the meshgrid
for dd = 1:31
I(Xd(dd),Yd(dd))=Zd(dd);
end
Zd = [Zd; zeros(70,1)];
Xd = [Xd; zeros(70,1)];
Yd = [Yd; zeros(70,1)];
[XX,YY] = meshgrid(1:101,1:101);
z = griddata(Xd,Yd,Zd,XX,YY,'cubic');
contourf(z)
=========================================
The resulting plot of this code is
http://s16.postimg.org/s7a627s5h/Graph.jpg
I would like some help to remove this "tail" from my graph and a sugggestion of how to draw a head + nose on the same picture (only if it is possible to plot this kind of graph).
I don't have enough reputation to comment above but you can set your own custom locations in EEGlab as far as I remember. Have you looked at the function writelocs? Maybe that helps. EEGlab's topoplots include nose and ears.
http://sccn.ucsd.edu/eeglab/allfunctions/writelocs.html

Importing CSV into Matlab

Chemical composition of a certain material
Hi,
I am trying to import the below mentioned data in CSV format in matlab, which is [1000x10] in dimensions.
HCL;H2SO4;CH4; SULPHUR;CHLORINE;S2O3;SO2;NH3;CO2;O2
144 2 3 141 140 6 7 137 136 10 11 133
13 131 130 16 17 127 126 20 21 123 122 24
25 119 118 28 29 115 114 32 33 111 110 36
108 38 39 105 104 42 43 101 100 46 47 97
96 50 51 93 92 54 55 89 88 58 59 85
61 83 82 64 65 79 78 68 69 75 74 72
73 71 70 76 77 67 66 80 81 63 62 84
60 86 87 57 56 90 91 53 52 94 95 49
48 98 99 45 44 102 103 41 40 106 107 37
109 35 34 112 113 31 30 116 117 27 26 120
121 23 22 124 125 19 18 128 129 15 14 132
12 134 135 9 8 138 139 5 4 142 143 1
I am able to import this data through my code
fid = fopen(uigetfile('.csv'),'rt');
FileName = fopen(fid);
headers = fgets(fid); %get first line
headers = textscan(headers,'%s','delimiter',';'); %read first line
format = repmat('%f',1,size(headers{1,1},1)); %count columns n makeformat string
data = textscan(fid,format,'delimiter',';'); %read rest of the file
data = [data{:}];
I am getting data in matrix form in variable data [1000x10] and name of all the components like HCL, H2SO4 in a cell array named headers{1x1}.
Now I have two questions like the built in import feature in matlab you have flexibility to import data as separate column vectors, numeric matrix,cell array and table format. Is it possible to do as such through code, like i get column vectors with their name HCL with [1000x1] and H2sO4 with [1000x1] in my workspace after import and so on all the column vectors with their names with [1000x1]dimensions.
if yes then help me please...?
If above mentioned is not possible then i can do alternatively that now I have names of column vectors in headers cell array, how I can extract those name and use those names as column vector names through code and I can assign data from data matrix [1000x10] to each column vector with their corresponding names.
like if i say
x = headers {1*1}{1*1}; i will get x = "HCL"
x = genvarname(x); I will get x= x0x22HCL0x2 BUT
I want that x get replaced with HCL.and then I assign
HCL = data(:,1) and same like this other variables H2SO4,SULPHUR, CHLORINE.
You can say i try to implement the import feature of column vector through my code.
Kindly help me to solve this issue. thanks
Have you tried the built-in readtable function?
You can access each column of the table by using the named column header.
If you'd like, you can use the two data types to create a table in MatLab. I'm not terribly familiar with its use, but it seems to be well documented. I'm sure someone else can expand upon this.
Edit:
After re-reading your question, I think this is closer to what you are after.
n=10;
what='HCL';%change this to any of the strings you interested in
numstr = repmat('%f',1,n);
hdrstr = repmat('%s',1,n);
headers = textscan(headers,hdrstr,'delimiter',';');
headers = headers(1,:)
data = cell2mat(textscan(fid,numstr,'delimiter',';'));
datout = data(:,strcmp(headers,what));%datout will be 1000x1 HCL data
Depending on what you want to do, you can loop through these appropriately
I know this is not what you asked for, but I would convert to a struct:
x=cell2struct(num2cell(data),headers,2)
reason is simple, selecting for example the third row with individual variables is not possible. With a struct simply use x(3)
If at some point you need the vectors you originally asked for and you can't use the strcut, use [x.HCL]

Matlab: 3D Surface Plot of values from own vectors

I have 3 vectors with values for coordinates (X,Y,Z) and I want to plot them as a surface. I have tried all sorts of solutions from here and other forums and cannot for the life of me get it to look like anything that makes sense. I have a picture that can describe the situation better but I can't post it as I don't have enough reputation. Please help. Thank you.
EDIT: This is the link to the picture :
In the picture, the origin of each vector arrow represents the point in a 3D coordinate, with the length of the vector arrow giving the magnitude of the required torque to move at that point in 3D space.
Right now, the data is separated in line vectors with each point's coordinate: so that's one vector for the X, one for Y. Z is one line inside a 3 line matrix as the whole matrix describes the required torque in a 3 coordinate axes.
I've tried using meshgridon the X and Y vectors and then attributed the Z value using griddataand then surfbut I'm not getting something that looks like the original:
The plot is linear but I'm pretty sure the data is not... at least not that linear.
It seems like you want want to plot the single points decribed by the vectors, here you have an old answer of mine (as a bonus you will have nice colorful plots 'cause that's what the original question asked):
Assuming Data=[Vec1,Vec2,Vec3,...] and VecN=[Xn,Yn,Zn]'
"
If you want to plot points, you can define an RGB color and plot single points with hold on like this:
hold on
for i=1:length(Data(:,1))
plot3(Data(i,1),Data(i,2),Data(i,3),'Color',[(i/100*255)/255 0/255 (255-(i/100*255))/255],'LineWidth',2)
end
shg
"
Managed to solve the problem. As mentioned, the data was not uniform and because of that, surfwas jumping from one end of the plot to the other, creating a total mess. Solved it by organising the values linearly using linspaceand then using those values to create the meshgrid and then assign the Z values using griddata with a cubic interpolation.. Managed to produce proper looking surface plots with the data on hand.
Plot each data point separately within a loop:
figure; hold on; grid on;
for i = 1:length(x)
stem3(x(i),y(i),z(i));
end
Don't forget to add hold on. Use "Rotate 3D" button from the toolbar in the figure window if the plot was shown in 2D at first.
You can use griddata parameter to natural, cubic or v4. This will do interpolation and make your graph with non-uniform data smooth
Below is a sample MATLAb code
x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

How can I retrieve the coordinate from excel and plot the points according to the map? - MATLAB

I'm currently working on map routing system for pedestrian pathway and not sure does anyone did this before. But I have collected various way-points coordinates of the path using google earth.
Below data are the coordinates points for the pathway which is save it on Excel and import to Matlab
1 1.37723400000000 103.839645000000
2 1.37722000000000 103.839741000000
3 1.37723300000000 103.839843000000
4 1.37723600000000 103.839968000000
5 1.37724100000000 103.840125000000
6 1.37723900000000 103.840245000000
7 1.37724900000000 103.840435000000
8 1.37725700000000 103.840674000000
9 1.37724800000000 103.840896000000
10 1.37726500000000 103.841071000000
11 1.37728300000000 103.841190000000
12 1.37729500000000 103.841393000000
13 1.37734600000000 103.841591000000
14 1.37736500000000 103.841918000000
15 1.37739800000000 103.842093000000
16 1.37742900000000 103.842400000000
17 1.37744900000000 103.842629000000
18 1.37748100000000 103.842895000000
19 1.37750200000000 103.843164000000
20 1.37752800000000 103.843428000000
21 1.37756500000000 103.843689000000
22 1.37757000000000 103.843997000000
23 1.37758500000000 103.844248000000
24 1.37749200000000 103.844499000000
25 1.37765800000000 103.844515000000
26 1.37806100000000 103.844467000000
27 1.37853700000000 103.844386000000
28 1.37888000000000 103.844349000000
29 1.37924600000000 103.844303000000
30 1.37957500000000 103.844266000000
31 1.37966200000000 103.844190000000
32 1.37965300000000 103.844034000000
33 1.37963800000000 103.843813000000
34 1.37961600000000 103.843550000000
35 1.37959500000000 103.843313000000
36 1.37957600000000 103.843097000000
37 1.37957300000000 103.843007000000
38 1.37956800000000 103.842852000000
39 1.37953900000000 103.842602000000
40 1.37949900000000 103.842315000000
41 1.37948700000000 103.842056000000
42 1.37953800000000 103.841882000000
43 1.37967800000000 103.841755000000
44 1.37984000000000 103.841674000000
45 1.38007300000000 103.841546000000
46 1.38027900000000 103.841430000000
47 1.38044400000000 103.841354000000
48 1.38064800000000 103.841248000000
49 1.38092400000000 103.841100000000
50 1.38125600000000 103.840942000000
51 1.38178300000000 103.840682000000
52 1.38202300000000 103.840565000000
53 1.38236700000000 103.840396000000
54 1.38271600000000 103.840206000000
55 1.38302800000000 103.840017000000
56 1.38314600000000 103.840222000000
57 1.38299000000000 103.840230000000
58 1.38286700000000 103.840288000000
59 1.38265300000000 103.840398000000
60 1.38242100000000 103.840522000000
61 1.38219900000000 103.840638000000
62 1.38195100000000 103.840766000000
63 1.38162100000000 103.840934000000
64 1.38130200000000 103.841093000000
65 1.38106700000000 103.841202000000
66 1.38088500000000 103.841286000000
67 1.38069300000000 103.841372000000
68 1.38049000000000 103.841494000000
69 1.38018700000000 103.841659000000
70 1.37988900000000 103.841822000000
71 1.37966700000000 103.841963000000
72 1.37965000000000 103.842037000000
73 1.37965100000000 103.842220000000
74 1.37966300000000 103.842363000000
75 1.37968900000000 103.842641000000
76 1.37973000000000 103.843049000000
77 1.37975300000000 103.843305000000
78 1.37978200000000 103.843690000000
79 1.37982300000000 103.844135000000
80 1.37989800000000 103.844222000000
81 1.37996400000000 103.844230000000
82 1.38029900000000 103.844183000000
83 1.38080600000000 103.844091000000
84 1.38119600000000 103.843985000000
85 1.38170500000000 103.843838000000
86 1.38194900000000 103.843765000000
87 1.38220500000000 103.843683000000
88 1.38250000000000 103.843564000000
89 1.38296800000000 103.843388000000
90 1.38367400000000 103.843107000000
91 1.38379700000000 103.842994000000
92 1.38384900000000 103.842791000000
93 1.38376700000000 103.842173000000
94 1.38372500000000 103.841758000000
95 1.38358400000000 103.841212000000
96 1.38350400000000 103.840867000000
97 1.38324600000000 103.840388000000
98 1.38288200000000 103.839785000000
99 1.38265700000000 103.839436000000
100 1.38238700000000 103.838973000000
101 1.38224900000000 103.838785000000
102 1.38215800000000 103.838830000000
103 1.38197900000000 103.838933000000
104 1.38162900000000 103.839097000000
105 1.38115400000000 103.839338000000
106 1.38080900000000 103.839609000000
107 1.38036600000000 103.839756000000
108 1.37995000000000 103.839782000000
109 1.37957400000000 103.839826000000
110 1.37904400000000 103.839883000000
111 1.37844300000000 103.839807000000
112 1.37803700000000 103.839743000000
113 1.37765800000000 103.839690000000
114 1.37735600000000 103.839665000000
115 1.38310100000000 103.840134000000
116 1.38108800000000 103.841146000000
117 1.37961700000000 103.842951000000
118 1.37978200000000 103.844233000000
My Code
M = xlsread('YCKMap.xlsx');
figure(),plot(M(:,2), M(:,3));
RESULT
http://i.stack.imgur.com/0dncY.jpg
What I want:
Is it possible to join these points together
(55 115 56) (49 116 65) (37 117 76) (30 118 80)
As shown in the image above?
I have figure out the answer for my question and would like to benefit the community for what I have come out with. According #Dan code plot(M(:,2), M(:,3); can plot the points out but the orientation of the graph is way off. Therefore I use waypoints function to make it better.
Anyway, organize your excel data by leaving a cell gap when you want to insert a new line.
I have paste the excel data for you to get a clearer picture.
Coding
M = xlsread('excel.xlsx');
waypoints =[M(:,2), M(:,3)];
[lttrk,lntrk] = track('rh',waypoints,'degrees');
figure(),geoshow(lttrk,lntrk,'DisplayType','line', 'color','r');
Excel Data
1 1.377234 103.839645
2 1.37722 103.839741
3 1.377233 103.839843
4 1.377236 103.839968
5 1.377241 103.840125
6 1.377239 103.840245
7 1.377249 103.840435
8 1.377257 103.840674
9 1.377248 103.840896
10 1.377265 103.841071
11 1.377283 103.84119
12 1.377295 103.841393
13 1.377346 103.841591
14 1.377365 103.841918
15 1.377398 103.842093
16 1.377429 103.8424
17 1.377449 103.842629
18 1.377481 103.842895
19 1.377502 103.843164
20 1.377528 103.843428
21 1.377565 103.843689
22 1.37757 103.843997
23 1.377585 103.844248
24 1.377492 103.844499
25 1.377658 103.844515
26 1.378061 103.844467
27 1.378537 103.844386
28 1.37888 103.844349
29 1.379246 103.844303
30 1.379575 103.844266
31 1.379662 103.84419
32 1.379653 103.844034
33 1.379638 103.843813
34 1.379616 103.84355
35 1.379595 103.843313
36 1.379576 103.843097
37 1.379573 103.843007
38 1.379568 103.842852
39 1.379539 103.842602
40 1.379499 103.842315
41 1.379487 103.842056
42 1.379538 103.841882
43 1.379678 103.841755
44 1.37984 103.841674
45 1.380073 103.841546
46 1.380279 103.84143
47 1.380444 103.841354
48 1.380648 103.841248
49 1.380924 103.8411
50 1.381256 103.840942
51 1.381783 103.840682
52 1.382023 103.840565
53 1.382367 103.840396
54 1.382716 103.840206
55 1.383028 103.840017
56 1.383146 103.840222
57 1.38299 103.84023
58 1.382867 103.840288
59 1.382653 103.840398
60 1.382421 103.840522
61 1.382199 103.840638
62 1.381951 103.840766
63 1.381621 103.840934
64 1.381302 103.841093
65 1.381067 103.841202
66 1.380885 103.841286
67 1.380693 103.841372
68 1.38049 103.841494
69 1.380187 103.841659
70 1.379889 103.841822
71 1.379667 103.841963
72 1.37965 103.842037
73 1.379651 103.84222
74 1.379663 103.842363
75 1.379689 103.842641
76 1.37973 103.843049
77 1.379753 103.843305
78 1.379782 103.84369
79 1.379823 103.844135
80 1.379898 103.844222
81 1.379964 103.84423
82 1.380299 103.844183
83 1.380806 103.844091
84 1.381196 103.843985
85 1.381705 103.843838
86 1.381949 103.843765
87 1.382205 103.843683
88 1.3825 103.843564
89 1.382968 103.843388
90 1.383674 103.843107
91 1.383797 103.842994
92 1.383849 103.842791
93 1.383767 103.842173
94 1.383725 103.841758
95 1.383584 103.841212
96 1.383504 103.840867
97 1.383246 103.840388
98 1.382882 103.839785
99 1.382657 103.839436
100 1.382387 103.838973
101 1.382249 103.838785
102 1.382158 103.83883
103 1.381979 103.838933
104 1.381629 103.839097
105 1.381154 103.839338
106 1.380809 103.839609
107 1.380366 103.839756
108 1.37995 103.839782
109 1.379574 103.839826
110 1.379044 103.839883
111 1.378443 103.839807
112 1.378037 103.839743
113 1.377658 103.83969
114 1.377356 103.839665
1 1.377234 103.839645
49 1.380924 103.8411
116 1.381088 103.841146
65 1.381067 103.841202
37 1.379573 103.843007
117 1.379617 103.842951
76 1.37973 103.843049
30 1.379575 103.844266
118 1.379782 103.844233
80 1.379898 103.844222
If you want to plot points in Matlab in a different order from that which they appear in your matrix, append a column vector at the beginning of the matrix that is a vector of the order the point should appear in. Then use the sortrows function of matlab to rearrange the matrix. It will now be in the correct order for plotting. For example:
M = [0 4 2; 1 17 5]';
plot(M);
Now suppose you knew that the correct plot order was (0,1) then (2,5) then (4,17). Compared to the input matrix, M, the correct plot order can be specified by order = [1 3 2]'
So to plot in the correct order:
M = [order'; M'];
M = sortrows(M);
plot(M(:,2), M(:,3);
Which means as long as you know the correct plot order, you can now use this method to get the correct plot.
So in your example you want those points to be plotted in this order (55 115 56) (49 116 65) (37 117 76) (30 118 80). Lets assume you want them to start at 55 as this is the first point in your new order, currently M looks like this: M = [...;30 118 80;...;37 117 76;...;49 116 65;...;55 115 56;...] but you want it to look like this: M = [...; 55 115 56; 56 116 65; 57 117 76; 58 118 80;...]. The problem is that you can't just change the values like this M(M(:,1) == 30,1) = 58 because then you will land up with 2 points indexed at 58. So first you need to add 1 too all the indices that are 58 or higher, so M(M(:,1) >= 58) = M(M(:,1) >= 58) + 1 and then finally M(M(:,1) == 30,1) = 58. So to generalise the changing of a point:
M(M(:,1) >= newIndex) = M(M(:,1) >= newIndex) + 1;
M(M(:,1) == oldIndex,1) = newIndex;
If there is some logic that define how you change the points, then you can write this into a loop. If not and it is just those 4 points, then just run this manually in the command line.