I have a problem to set controller by keyboard arrows in matlab app designer.
I cant reset my key value when there is no key Pressed.
I want to make action only while some key is pressing and stop the action when the key is un pressing
function ControllerUIFigureKeyPress(app, event)
key = event.Key;
switch key
case 'leftarrow'
app.left_icon.Position = [210 98 71 102];
case 'uparrow'
app.up_icon.Position = [285 150 71 102];
case 'rightarrow'
app.right_icon.Position = [360 98 71 102];
case 'downarrow'
app.down_icon.Position = [285 40 71 102];
otherwise
app.down_icon.Position = [285 60 71 102];
app.up_icon.Position = [285 133 71 102];
app.right_icon.Position = [343 98 71 102];
app.left_icon.Position = [225 98 71 102];
end
Related
mat =
147 155 139 104 84 139
136 134 99 73 60 144
98 82 60 54 47 118
86 59 46 48 38 90
88 66 50 44 35 67
88 75 53 40 43 48
p = mat(3,3)
q = mat(2,5)
V = [1:60]
all i want is to check if there exist a path between pixel p and q using vector V, for example given matrix above there exist a paths: path1: (3,3) , (3,4) , (3,5) , (2,5) path2: (3,3) , (4,3) , (4,4) , (4,5) , (3,5) , (2,5) and many other paths, Note: pixel value in each path's coordinate should be in 'V' and obviously p and q should also be in 'V'. that's what i am trying to achieve in MATLAB
Code i've written so far:
mat =
147 155 139 104 84 139
136 134 99 73 60 144
98 82 60 54 47 118
86 59 46 48 38 90
88 66 50 44 35 67
88 75 53 40 43 48
p = mat(3,3)
q = mat(2,5)
V = [1:60]
% need to check N4 connectivity through pixel value p to q
cc = bwconncomp(mat,4)
for i=1:cc.NumObjects
pix = cc.PixelIdxList{i}
if any(ismember(pix(:), p)) && any(ismember(pix(:), q)) && any(ismember(V(:), p)) && any(ismember(V(:), q))
display('found N4 connectivity')
end
Actual Question: is this correct way to do the required task or am i doing it wrong?
I am programming a Lego brick NXT in Matlab to go through a maze. The robot is supposed to drive forward until the touch sensor is activated by a wall.
The robot should
stop
backup
stop
read the ultraSonic (distance) sensor
display the result
If the sensor reads a large distance, the robot turns right and stops and if the sensor reads a small distance, the robot turns left and stops.
Then, the robot continues forward again until it runs into another wall.
Current code:
The robot will hit the wall, stop, back up, stop, and it will display that it’s turning, but it doesn’t turn.
Is there something I am typing incorrectly? Should I initialize the motors before the loop or inside of the loop? I am especially wary of my pauses.
1 %B = NXTMotor('B');
2 %C = NXTMotor('C');
3 OpenSwitch(SENSOR_1);
4 OpenUltrasonic(SENSOR_4);
5
6
7 while 1
8
9 B = NXTMotor('B');
10 C = NXTMotor('C');
11
12 %FORWARD 1
13 disp('move forward');
14 B.Power = 50;
15 C.Power = 50;
16
17 B.SendToNXT();
18 C.SendToNXT();
19 B.WaitFor();
20 C.WaitFor();
21 disp('before getswitch');
22
23 if GetSwitch(SENSOR_1)
24 pause(0.2);
25 B.Power = 0;
26 C.Power = 0;
27 B.SendToNXT();
28 C.SendToNXT();
29 B.WaitFor();
30 C.WaitFor();
31 disp('Hit a wall and stops');
32
33 disp('back up');
34 B.Power = -50;
35 C.Power = -50;
36
37 B.TachoLimit = 720;
38 C.TachoLimit = 720;
39
40 B.SendToNXT();
41 C.SendToNXT();
42 B.WaitFor();
43 C.WaitFor();
44
45 pause(0.5);
46 B.Power = 0;
47 C.Power = 0;
48 B.SendToNXT();
49 C.SendToNXT();
50 B.WaitFor();
51 C.WaitFor();
52 disp('finished back up');
53
54 %ultrasonic
55 distance = GetUltrasonic(SENSOR_4);
56 disp(distance);
57
58 if distance >100
59 %if GetUltrasonic(SENSOR_4) > 100
60
61 %TURN 1
62 disp('turn1 > 100');
63
64 B.Power = 0;
65 C.Power = 100;
66
67 B.TachoLimit = 720;
68 C.TachoLimit = 720;
69
70 B.SendToNXT();
71 C.SendToNXT();
72 B.WaitFor();
73 C.WaitFor();
74 disp('turn1 finished');
75
76 pause(0.2);
77 B.Power = 0;
78 C.Power = 0;
79 B.SendToNXT();
80 C.SendToNXT();
81 B.WaitFor();
82 C.WaitFor();
83
84 end
85
86 if distance <= 100
87
88 disp('turn2 < 100');
89
90 B.Power = 100;
91 C.Power = 0;
92 disp('turn 2');
93 B.TachoLimit = 720;
94 C.TachoLimit = 720;
95
96 B.SendToNXT();
97 C.SendToNXT();
98 B.WaitFor();
99 C.WaitFor();
100 disp('Finished turn 2');
101
102 pause(0.5);
103 B.Power = 0;
104 C.Power = 0;
105 B.SendToNXT();
106 C.SendToNXT();
107 B.WaitFor();
108 C.WaitFor();
109
110 disp('stopped turn2 and ready to go forward');
111
112 end
113
114 B.Stop('off');
115 C.Stop('off');
116 end
117
118 %B.Stop('off');
119 %C.Stop('off');
120 end
121
122 CloseSensor(SENSOR_4);
123 CloseSensor(SENSOR_1);
I have an error in the following forloop. I know because the end value of the first for is going to be changed and it is not acceptable for Matlab to change in inside iteration. But would you have any idea how to overcome to it? By the way I used while, but does not help me at all. Data are as follow:
D = [
2.39484592826072e-05 286
4.94140791861196e-05 161
5.07906972800045e-05 163
0.000103133134300751 141
0.000142755898501384 136
0.000143741615840070 152
0.000188072960663613 177
0.000203545320971960 1
0.000269110781516704 296
0.000333161025069404 293
0.000351184122591795 167
0.000393661764751196 299
0.000469154814856272 173
0.000516662289403544 181
0.000537612407901054 156
0.000698464342131732 246
0.000848447859349023 66
0.000875283151707512 75
0.00102377583629824 68
0.00110034589129900 277
0.00110693756077989 129
0.00120680501123819 87
0.00151080017572355 78
0.00159156469379168 248
0.00190852817897233 270
0.00192106167039306 133
0.00224677708557380 258
0.00246430115488258 264
0.00288772180685041 255
0.00299392149856582 81
0.00315341807121748 242
0.00327625233716732 27
0.00362308575885149 124
0.00434568780796603 220
0.00443389247698617 239
0.00470947127244510 60
0.00474015278667278 23
0.00481651908877289 230
0.00487750364266560 53
0.00510342992049100 56
0.00513758569662983 228
0.00515453564704144 121
0.00515656244518627 232
0.00526922882200147 8
0.00547349131456174 50
0.00553337871530176 117
0.00569159206242299 18
0.00620144292620718 13
0.00630382865700000 119
0.00755647842280271 92
0.00983041839684126 40
0.00997057619578698 98
0.0102611966834032 44
0.0103337998140422 100
0.0105132461082006 37
0.0106952804631761 109
0.0107424055503829 208
0.0109630950142485 111
0.0115094667290339 105
0.0119529682389369 107];
ymin= D(:,1);
mean_value = 0.00773867192661190;
criteria = min(ymin);
kk = 1;
diff = 60;
and here is the code that I would have an error for the changing size_D which is expected.
while criteria < mean_value
if isempty(B)
ind_crt = find(min(ymin));
B(kk,:) = D(ind_crt,:);
D(ind_crt,:) = [];
kk = kk + 1;
end
criteria = min(min(D));
size_D = size(D,1);
for ii=1:size_D
if D(ii,1) == criteria
size_B = size(B,1);
for jj = 1:size_B
if abs(D(ii,2) - B(jj,2)) > diff
B(kk,:) = D(ii,:);
D(ii,:)= [];
kk = kk + 1;
end
size_D = size_D -1;
criteria = min(min(D))
end
end
end
end
Update:
Here is the error:
Attempted to access D(59,1); index out of bounds because
size(D)=[58,2].
Error in local_minima (line 50)
if D(ii,1) == criteria
Replace your for loop by a while loop, so that the code in the loop is run only if the condition ii<=size_D is verified:
ii=0;
while ii<=size_D
ii=ii+1;
%loop code
instead of the
for ii=1:size_D
%loop code
How can I get the coordinates at a specific point?
I want to get the X coordinate of the points with Y = 18.1 ; Y = 33; Y = 70
Those points need to lie on the function that I plot.
Sample Code
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y) , grid on
Unfortunately, you are trying to find the values of t associated with a y, and your function is not monotonic so we need to actually code up a mock linear interpolation. Note, there may be a better way, but I do not know of it right now. Try the following code where yVals are the values you want an associated t for, and possArray will include all values of t that may satisfy those conditions.
clc; close all; clear all;
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y)
grid on
hold on
yVals = [18.1,33,70,222.5,230];
possArray = cell(1,numel(yVals));
iter = 1;
for val = yVals;
poss = [];
possNum = 1;
for i = 1:numel(y)-1
if y(i) <= val && y(i+1) >= val
minDiff = val-y(i);
yDiff = y(i+1)-y(i);
percAlong = minDiff/yDiff;
poss(possNum) = (t(i+1)-t(i))*percAlong+t(i);
possNum = possNum+1;
end
end
possArray{iter} = poss;
iter = iter + 1;
end
colors = hsv(numel(yVals));
legendCell = cell(numel(yVals)+1,1);
legendCell{1} = 'Original Line';
for i = 1:numel(yVals)
plot(possArray{i},yVals(i)*ones(size(possArray{i})),...
'x','MarkerSize',10,'LineWidth',2,'Color',colors(i,:))
legendCell{i+1} = ['Values for Y = ' ,num2str(yVals(i))];
end
legend(legendCell)
hold off
As stated previously, this is linear interpolation, so if you need it to be more complicated that is on you, the concept should however be similar
UPDATE
Updated code above to be a little more clean, and added a plot indicating that multiple possibilities may arise for a single value, and that the code will return all possibilities.
I want to write a sliding window algorithm for use in activity recognition.
The training data is <1xN> so I'm thinking I just need to take (say window_size=3) the window_size of data and train that. I also later want to use this algorithm on a matrix
.
I'm new to matlab so i need any advice/directions on how to implement this correctly.
The short answer:
%# nx = length(x)
%# nwind = window_size
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
idx will be a matrix of size nwind-by-K where K is the number of sliding windows (ie each column contains the indices of one sliding window).
Note that in the code above, if the last window's length is less than the desired one, it is dropped. Also the sliding windows are non-overlapping.
An example to illustrate:
%# lets create a sin signal
t = linspace(0,1,200);
x = sin(2*pi*5*t);
%# compute indices
nx = length(x);
nwind = 8;
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
%'# loop over sliding windows
for k=1:size(idx,2)
slidingWindow = x( idx(:,k) );
%# do something with it ..
end
%# or more concisely as
slidingWindows = x(idx);
EDIT:
For overlapping windows, let:
noverlap = number of overlapping elements
then the above is simply changed to:
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1;
An example to show the result:
>> nx = 100; nwind = 10; noverlap = 2;
>> idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1
idx =
1 9 17 25 33 41 49 57 65 73 81 89
2 10 18 26 34 42 50 58 66 74 82 90
3 11 19 27 35 43 51 59 67 75 83 91
4 12 20 28 36 44 52 60 68 76 84 92
5 13 21 29 37 45 53 61 69 77 85 93
6 14 22 30 38 46 54 62 70 78 86 94
7 15 23 31 39 47 55 63 71 79 87 95
8 16 24 32 40 48 56 64 72 80 88 96
9 17 25 33 41 49 57 65 73 81 89 97
10 18 26 34 42 50 58 66 74 82 90 98