I've been working on a macro (with activex button) for about 5 hours. I need it to be able to automatically sort new data according to facility when it's added, and then to put it into a pivot and open new tabs with different facility data. I have ran into every compile error in the book. I've followed some advice and now I'm stuck on an error which says: Run-Time error '9', Subscript out of range. I am a beginner and so please forgive me if this is a less than intelligent question. I'm going to post the code.
'
' CreateWorkList Macro
' Create a work list by the facility
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Cells.Select
ActiveSheet.Range("$C$1:$W$2897").AutoFilter Field:=2, Criteria1:="2"
Selection.Copy
ActiveSheet.Range("$C$1:$W$2897").AutoFilter Field:=2
Range("C140").Select
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 19
ActiveWindow.ScrollColumn = 21
ActiveWindow.ScrollColumn = 24
ActiveWindow.ScrollColumn = 25
ActiveWindow.ScrollColumn = 26
ActiveWindow.ScrollColumn = 27
ActiveWindow.ScrollColumn = 28
ActiveWindow.ScrollColumn = 30
ActiveWindow.ScrollColumn = 31
ActiveWindow.ScrollColumn = 32
ActiveWindow.ScrollColumn = 36
ActiveWindow.ScrollColumn = 37
ActiveWindow.ScrollColumn = 40
ActiveWindow.ScrollColumn = 42
ActiveWindow.ScrollColumn = 44
ActiveWindow.ScrollColumn = 45
ActiveWindow.ScrollColumn = 46
ActiveWindow.ScrollColumn = 47
ActiveWindow.ScrollColumn = 54
ActiveWindow.ScrollColumn = 55
ActiveWindow.ScrollColumn = 57
ActiveWindow.ScrollColumn = 60
ActiveWindow.ScrollColumn = 63
ActiveWindow.ScrollColumn = 65
ActiveWindow.ScrollColumn = 69
ActiveWindow.ScrollColumn = 71
ActiveWindow.ScrollColumn = 77
ActiveWindow.ScrollColumn = 79
ActiveWindow.ScrollColumn = 63
ActiveWindow.ScrollColumn = 1
Range("C1:W1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R135C21", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(1, 26).Select
ActiveWindow.SmallScroll ToRight:=5
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Campus")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPID")
.Orientation = xlRowField
.Position = 1
End With
ActiveWindow.SmallScroll ToRight:=6
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Campus")
.PivotItems("Celebration").Visible = False
.PivotItems("East Orlando").Visible = False
.PivotItems("Kissimmee").Visible = False
.PivotItems("Orlando").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("AcctNum"), "Sum of AcctNum", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of AcctNum")
.Caption = "Counts of AcctNum"
.Function = xlCount
End With
Range("AA10").Select
Selection.ShowDetail = True
Next line is where I get the error:
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Altamonte"
Sheets("Sheet1").Select
Range("AB10").Select
Selection.ShowDetail = True
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "Apopka"
Sheets("Sheet1").Select
Range("AC10").Select
Selection.ShowDetail = True
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Sheet1"
Sheets("Sheet4").Select
Sheets("Sheet4").Name = "Winter Park"
Range("C21").Select
Related
I am trying to apply dct and quantization to 8*8 blocks like this.
img = imread('frames/frm001.png');
ycbcr = rgb2ycbcr(img);
y = ycbcr(:,:,1);
func = #dct2;
whos func;
d1 = blkproc(y,[8 8],func);
qmtx = [16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
d1 = ceil(d1);
whos d1;
c = #(block_struct) (block_struct.data) ./ qmtx;
d2 = blkproc(d1,[8,8], c );
whos d2;
For this I'm getting the following error
Attempt to reference field of non-structure array.
Error in #(block_struct)(block_struct.data)./qmtx
Error in blkproc (line 103)
firstBlock = feval(fun,x,params{:});
Error in dct (line 28)
d2 = blkproc(d1,[8,8], c );
What might have gone 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);
lets say
g(:,:,1) = [ 1; 4; 7]
g(:,:,2) = [11; 44; 77]
g(:,:,3) = [111; 444; 777] .
Lets say a = [2; 3; 1] and b = [1; 3; 2]. I want the output like this
[4;777;11]. first element is g(2,:,1) , second element is g(3,:,3) and third element is g(1,:,2).
It's as simple as this -
[m,n,r] = size(g)
out = g(a + (b-1)*m*n)
For a generic case, when you want to specify the column number as well -
out = g(a + (col_num-1)*m + (b-1)*m*n)
For a more generic case, when you want to specify more than just one column -
g(bsxfun(#plus,(col_nums-1)*m,a(:)+(b(:)-1)*m*n))
For even more generic cases, you have to ask harder questions.
Sample run -
>> g
g(:,:,1) =
11 81 26 19 87
96 87 80 27 58
1 9 43 15 55
77 40 91 14 15
g(:,:,2) =
85 40 19 90 34
62 8 24 94 90
35 24 42 49 37
51 13 5 49 12
g(:,:,3) =
78 10 57 82 65
39 14 6 2 73
24 94 24 5 65
40 95 35 17 45
>> [m,n,r] = size(g);
>> a = [2,3,1]; b = [1,3,2];
>> col_nums = [1 3];
>> g(bsxfun(#plus,(col_nums-1)*m,a(:)+(b(:)-1)*m*n))
ans =
96 80
24 24
85 19
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What's the best way to calculate min/avg/max/std-dev for some random data in shell?
What if one has several columns per line, and needs to calculate the statistics for each one?
Sample input (based on processing of the hping output), with the columns 3, 4 and 5 being of interest:
0 145.5 146 = 75 + 71
1 142.7 142 = 72 + 70
2 140.7 140 = 70 + 70
3 146.7 146 = 76 + 70
4 148.3 148 = 77 + 71
5 157.5 157 = 87 + 70
6 167.1 167 = 96 + 71
7 166.3 166 = 95 + 71
8 167.7 167 = 97 + 70
9 159.0 159 = 88 + 71
10 156.7 156 = 86 + 70
11 154.9 155 = 84 + 71
12 151.9 152 = 81 + 71
13 157.3 157 = 86 + 71
14 155.0 155 = 84 + 71
15 157.7 158 = 87 + 71
16 156.6 156 = 86 + 70
(Note that this input is a live stream ad infinitum.)
I suggest you use Perl and keep a running total of N, Σx, and Σx², as well as the minimum and maximum x values. All of the values you need can be derived from those.
This example demonstrates. It dumps the current statistics after each line of the input is read.
use strict;
use warnings;
my ($n, #sum, #sumsq, #min, #max);
while (<DATA>) {
my #columns = /[0-9.]+/g;
my (#mean, #std_dev);
++$n;
for my $i (0 .. 2) {
my $x = $columns[$i + 2];
my $xsq = $x * $x;
$sum[$i] += $x;
$sumsq[$i] += $xsq;
$mean[$i] = $sum[$i] / $n;
$std_dev[$i] = sqrt($sumsq[$i]/$n - $mean[$i] * $mean[$i]);
$min[$i] = $x unless defined $min[$i] and $min[$i] <= $x;
$max[$i] = $x unless defined $max[$i] and $max[$i] >= $x;
}
print "min = #min\n";
print "max = #max\n";
print "mean = #mean\n";
print "std_dev = #std_dev\n";
print "---\n";
}
__DATA__
0 145.5 146 = 75 + 71
1 142.7 142 = 72 + 70
2 140.7 140 = 70 + 70
3 146.7 146 = 76 + 70
4 148.3 148 = 77 + 71
5 157.5 157 = 87 + 70
6 167.1 167 = 96 + 71
7 166.3 166 = 95 + 71
8 167.7 167 = 97 + 70
9 159.0 159 = 88 + 71
10 156.7 156 = 86 + 70
11 154.9 155 = 84 + 71
12 151.9 152 = 81 + 71
13 157.3 157 = 86 + 71
14 155.0 155 = 84 + 71
15 157.7 158 = 87 + 71
16 156.6 156 = 86 + 70
output
min = 146 75 71
max = 146 75 71
mean = 146 75 71
std_dev = 0 0 0
---
min = 142 72 70
max = 146 75 71
mean = 144 73.5 70.5
std_dev = 2 1.5 0.5
---
min = 140 70 70
max = 146 75 71
mean = 142.666666666667 72.3333333333333 70.3333333333333
std_dev = 2.4944382578501 2.05480466765642 0.47140452079146
---
min = 140 70 70
max = 146 76 71
mean = 143.5 73.25 70.25
std_dev = 2.59807621135332 2.38484800354236 0.433012701892219
---
min = 140 70 70
max = 148 77 71
mean = 144.4 74 70.4
std_dev = 2.93938769133971 2.60768096208109 0.489897948555485
---
min = 140 70 70
max = 157 87 71
mean = 146.5 76.1666666666667 70.3333333333333
std_dev = 5.40832691319598 5.39804491356711 0.47140452079146
---
min = 140 70 70
max = 167 96 71
mean = 149.428571428571 79 70.4285714285714
std_dev = 8.74817765279739 8.55235974119756 0.494871659305337
---
min = 140 70 70
max = 167 96 71
mean = 151.5 81 70.5
std_dev = 9.8488578017961 9.59166304662544 0.5
---
min = 140 70 70
max = 167 97 71
mean = 153.222222222222 82.7777777777778 70.4444444444444
std_dev = 10.4857339888036 10.3470637571759 0.496903995000609
---
min = 140 70 70
max = 167 97 71
mean = 153.8 83.3 70.5
std_dev = 10.0975244490914 9.94032192637645 0.5
---
min = 140 70 70
max = 167 97 71
mean = 154 83.5454545454545 70.4545454545455
std_dev = 9.64836302648838 9.50945592902742 0.497929597732158
---
min = 140 70 70
max = 167 97 71
mean = 154.083333333333 83.5833333333333 70.5
std_dev = 9.24173805202349 9.10547759440561 0.5
---
min = 140 70 70
max = 167 97 71
mean = 153.923076923077 83.3846153846154 70.5384615384615
std_dev = 8.89651218141581 8.77530154238378 0.498518515262866
---
min = 140 70 70
max = 167 97 71
mean = 154.142857142857 83.5714285714286 70.5714285714286
std_dev = 8.60943952761114 8.48287590817347 0.494871659305337
---
min = 140 70 70
max = 167 97 71
mean = 154.2 83.6 70.6
std_dev = 8.32025640630559 8.19593395125498 0.489897948558269
---
min = 140 70 70
max = 167 97 71
mean = 154.4375 83.8125 70.625
std_dev = 8.10839649684202 7.97824189593171 0.484122918275927
---
min = 140 70 70
max = 167 97 71
mean = 154.529411764706 83.9411764705882 70.5882352941177
std_dev = 7.874886718579 7.75712642546343 0.492152956783766
---
assume I have a 10x10 matrix M
M=[64 36 50 87 22 45 37 23 68 88;
33 23 87 49 54 25 35 98 78 52;
12 54 76 43 24 87 54 98 45 34;
77 87 23 45 34 65 23 76 12 76;
12 34 55 44 76 98 93 23 54 67;
22 55 78 90 88 56 34 23 12 76;
99 23 67 89 34 23 12 87 45 23;
22 54 76 89 65 23 45 12 93 12;
44 56 23 88 67 14 15 67 34 12;
11 44 77 99 34 23 78 34 12 79];
I want to first find out the local maximum in the matrix
and then according to the maximum position do a sum over a 3x3 region over M
For the first step, the code I used is local_max=imregionalmax(M). to find out the local maximum position, but how can I go further to use this coordination to sum over a 3x3 matrix over M?
Thanks for the help.
You can calculate the sum for the whole matrix and then only keep the values that you're interested in. This should work:
local_max=imregionalmax(M)
sums = imfilter(M, ones(3));
local_max_sums = sums(local_max);
And if what you want is a matrix with non-zero entries where the local maxima are located:
local_max_sums = sums .* local_max;
You seem to be looking for the matrix subset functionality of Matlab.
Basically, for
M = [ 1 2 3 4 5 6;
4 5 6 7 8 9;
7 8 9 0 1 2;
0 1 2 3 4 5;
3 4 5 6 7 8;
6 7 8 9 0 1];
If you have a max at (3,3), you can use M(2:4, 2:4) to get
N = [ 5 6 7;
8 9 0;
1 2 3];
Summing that matrix is all that remains - as simple as
total = sum(N(:));
This is kind of brute force for Matlab, but I think it works.
bw = imregionalmax(M);
[x,y] = find(bw);
s = [];
for i = 1:length(x)
startX = x(i)-2;
if(startX < 1)
startX = 1;
end
endX = x(i)+2;
if endX > 10
endX = 10;
end
startY = y(i)-2;
if startY < 1
startY = 1;
end
endY = y(i)+2;
if endY > 10
endY = 10;
end
s(i) = sum2(M(startX:endX, startY:endY));
end