I want to create a not-equally-spaced sequence of numbers in MATLAB starting from 24 and ending to 511.The Sequence uses 32 and 33 alternately as the increment. Thus, the sequence would be as below : [24 56 89 121 154 186 219 251 284 316 349 381 414 446 479 511] Notice that :
24+32=56
56+33=89
89+32=121
121+33=154
...
I just wonder how to modify my own codes or to write new codes to have the answer. My own codes are below:
t_3233=0;
for k=24:(32+t_3233):511
t_3233
k
if t_3233==1
t_3233=0;
else if t_3233==0
t_3233=1;
end
end
end
In this particular case you can use:
len = 16;
vector = round(linspace(24,511,len))
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
I am currently having trouble with formatting my output. I understand that this may be "bad practice", but using format is a requirement.
I am attempting to get a print out of the values that I retrieved from the database into a template of a SQL insert for Netcool.
I'm referencing http://perldoc.perl.org/perlform.html to apply the proper variables but I'm not sure why this is not working.
Here is a code snippet:
129 foreach my $row (#{$rows}) {
130
131 $trigger_name = removeNull($row->{triggername});
132 $trigger_group = $row->{groupname};
133 $trigger_kind = $row->{triggerkind};
134 $code_block = $row->{codeblock};
135 $isDebugEnabled = IntToLiteralBoolean($row->{debugenabled});
136 $isEnabled = IntToLiteralBoolean($row->{isenabled});
137 $priority = $row->{triggerpriority};
138 $comment_block = $row->{commentblock};
139 $evaluate_block = $row->{evaluateblock};
140 $bind_name = $row->{bindname};
141 $condition_block = $row->{conditionblock};
142 $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146 if($trigger_kind == 0) {
147 #database logic goes here
148 my $sql_getDatabaseFields = "select *
149 from catalog.database_triggers
150 where TriggerName = '$trigger_name'";
151
152 print "\n\nExtracting trigger: $trigger_name";
153 print "\nTriggerKind: Database, $trigger_kind";
154
155 my $sth_triggerType = $dbh->prepare($sql_getDatabaseFields);
156 $sth_triggerType->execute;
157 my $dbRows = $sth_triggerType->fetchall_arrayref({});
158
159 foreach my $row (#{$dbRows}) {
160 $event_order = $row->{eventorder};
161 $event_op = $row->{eventop};
162 $database_name = $row->{databasename};
163 $table_name = $row->{tablename};
164 $event_level = $row->{eventlevel};
165 write;
166
167 # print "\nEvent Order: $event_order";
168 # print "\nEvent Op: $event_op";
169 # print "\nDatabase Name:$database_name";
170 # print "\nTable name:$table_name";
171 # print "\nEvent Level: $event_level";
172 }
173 format =
174
175 CREATE OR REPLACE TRIGGER #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
176 $trigger_name
177 GROUP #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
178 $trigger_group
179 DEBUG #<<<<<<<<<<<<
180 $isDebugEnabled
181 ENABLED #<<<<<<<<<<<<
182 $isEnabled
183 PRIORITY #<<
184 $priority
185 COMMENT ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
186 $comment_block
187 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
188 $comment_block
189 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190 $comment_block
191 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
192 $comment_block
193 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194 $comment_block
195 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196
197 begin
198 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
199 $code_block
200 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
201 $code_block
202 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
203 $code_block
204 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
205 $code_block
206 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
207 $code_block
208 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
209 $code_block
210 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
211 $code_block
212 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
213 $code_block
214 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
215 $code_block
216 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
217 $code_block
218 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
219 $code_block
220 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221 $code_block
222 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
223 $code_block
224 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
225 $code_block
226 end;
227 .
This is the output when debugging:
Extracting trigger: service_reinsert
Not enough format arguments at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Modification of a read-only value attempted at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Debugged program terminated. Use q to quit or R to restart,
139: $evaluate_block = $row->{evaluateblock};
140: $bind_name = $row->{bindname};
141: $condition_block = $row->{conditionblock};
142==>b $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146: if($trigger_kind == 0) {
147 #database logic goes here
148: my $sql_getDatabaseFields = "select *
DB<2> x $code_block
0 "\cJ\cIset old.LastReportAt = new.LastReportAt;\cJ\cJ\cIif ( old.CurrentState != new.CurrentState )\cJ\cIthen\cJ\cI\cIset old.CurrentState = new.CurrentState;\cJ\cI\cIset old.StateChange = new.StateChange;\cJ\cIend if;\cJ\cJ\cIif ( new.CurrentState = 0 )\cJ\cIthen\cJ\cI\cIset old.LastGoodAt = new.LastGoodAt;\cJ\cIelseif ( new.CurrentState = 1 )\cJ\cIthen\cJ\cI\cIset old.LastMarginalAt = new.LastMarginalAt;\cJ\cIelseif ( new.CurrentState = 2 )\cJ\cIthen\cJ\cI\cIset old.LastBadAt = new.LastBadAt;\cJ\cIend if;\cJ\c#"
DB<4> x $comment_block
0 "Service processing for service.status\c#"
DB<5> c
Compare the following two examples:
1)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
$str
begin
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
mars
begin
hello
world
end;
2)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
begin #LINE 11
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
Modification of a read-only value attempted at 1.pl line 11.
My FPGA is sending UDP packets on network using 100 mbps ethernet and a have written a MATLAB code to capture the data. The problem is i am getting very low speed in MATLAB around 50 kbps during reception. FPGA kit is connected to a gbps switch and then to PC. No internet cable in the switch.
I am pasting the matlab code below. If i try to increase the speed by increasing buffer size, the packets are dropped. current settings are through hit and trial on which i receive all data successfully. IS there any way to increase data reception speed in MATLAB?
Code:: (UDP from FPGA to Matlab)
clc
clear all
close all
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off');
set(u, 'InputBufferSize', 18);
set(u,'Timeout',0.1);
fopen(u);
x=tic;
for i =1:1000
a(:,i) = fread(u,18);
end
fclose(u);
delete(u);
t=toc(x);
bw = (1000*18*8)/t;
/////////////////////////////////////////////////////////
A MODIFIED VERSION OF THE ABOVE CODE (EASE OF UNDERSTANDING) + IMAGE Showing the PROBLEM
also: An image showing Data Variable with a buffer size of 20 Packets (18 bytes / Packet). Data must not be all zero as pointed in the image. It represents missed packets.
/////////////////////////////////////////////////////////
clc
clear all
close all
packet_size = 18; % Size of 1 Packet
buffer_size = 1*packet_size; % Buffer to store 1024 packets each of Packet_Size
buffer_read_count = 10; % How many times the buffer must be read
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off');
set(u, 'InputBufferSize', buffer_size);
set(u,'Timeout',0.5);
fopen(u);
x=tic;
for i =1:buffer_read_count
[a, count] = fread(u,buffer_size); % Read the complete buffer in one Fread()
if (count == buffer_size)
data(:, i) = a; %If Read_BYtes(Count) == BufferSize Store in Data
end
end
fclose(u);
delete(u);
t=toc(x);
bw = (buffer_read_count*buffer_size*8)/t; %Speed / BW of UDP Reception
I looked at your code and found some basic corrections, let me know if it speed up your code.
u=udp('192.168.0.100','RemotePort',4660,'Localport',4661);
set(u,'DatagramTerminateMode','off', ...
'InputBufferSize', 18, ...
'Timeout',0.1); % I think only one call of set is needed here
fopen(u);
x=tic;
% The variable a is not pre-allocated before the loop
a = zeros(YourNumberOfLine, 1000)
for ii =1:1000 % Always use ii and jj and not i and j
a(:,ii) = fread(u,18);
end
fclose(u);
delete(u);
t=toc(x);
bw = (1000*18*8)/t;
Let me summarize my comments.
Low code efficiency
As #m_power has pointed out, using i and j slows down your code by a bit. See this for more information. In Matlab, you should always use ii and jj instead.
You didn't initialize data. See how Mathworks explain this. If #1 "slows by a bit", #2 then slows a lot.
Since your code is slow, it's not guaranteed that each time FPGA sends a packet, your PC is able to find any available buffer to receive the packet.
Full buffer
if (count == buffer_size)
data(:, i) = a; %If Read_BYtes(Count) == BufferSize Store in Data
end
So if the packet is smaller than the buffer, data(:,i) = nothing? That is the most possible reason why you are getting zeros in column 3,4,and 5.
Empty buffer
Zeros in column 3, 4 and 5 may also originate from an empty buffer, if you have done the previous changes. The buffer is not guaranteed to carry something when Matlab reads it, so some for iterations may catch zero-length contents, data(:,ii) = 0.
Use a while loop to solve this issue. Only count for non-empty buffer readings.
ii = 0;
while (ii < buffer_read_count)
[a, count] = fread(u, buffer_size);
if count % non-empty reading
ii = ii+1;
data(1:count,ii) = a;
end
end
....incomplete packets?
You wait for a full buffer, because each time you want to read an entire packet? I suddenly realized it; how stupid I was!
But what you have done is keeping reading the buffer, and throwing away the data as long as it's shorter than the buffer length.
Instead, you'll need aggregate the data in each loop.
data = zeros(buffer_size, buffer_read_count);
total_size = buffer_read_count*buffer_size;
ptr = 1; % 1-D array index of data
while (ptr < total_size)
[a, count] = fread(u, buffer_size);
if count % non-empty reading
if ( (ptr+count) > total_size )
data(ptr:end) = a(1:(total_size-ptr+1));
ptr = total_size;
else
data( ptr:(ptr+count-1) ) = a;
ptr = ptr+count;
end
end
end
Test - I changed fread to a random integer generator with ii remembering how many times the buffer is read.
clear all;clc;
buffer_size = 18;
buffer_read_count = 10;
data = zeros(buffer_size, buffer_read_count);
total_size = buffer_read_count*buffer_size;
ptr = 1; % 1-D array index of data
ii = 1;
while (ptr < total_size)
count = randi(buffer_size);
a = randi(9, count, 1) + ii*10; % 10's show number of buffer readings
ii = ii+1;
% [a, count] = fread(u, buffer_size);
if count % non-empty reading
if ( (ptr+count) > total_size )
data(ptr:end) = a(1:(total_size-ptr+1));
ptr = total_size;
else
data( ptr:(ptr+count-1) ) = a;
ptr = ptr+count;
end
end
end
disp(data)
The result is
13 38 51 63 72 93 104 125 141 164
12 35 53 63 73 96 101 123 148 168
14 33 55 68 72 99 106 124 142 168
14 37 51 69 77 91 109 127 145 165
12 33 57 66 76 96 114 137 143 168
14 39 56 63 72 94 117 139 144 169
11 46 55 61 72 93 111 139 146 164
16 42 58 68 75 93 119 135 153 164
26 41 58 66 79 109 126 139 152 166
33 43 58 69 75 102 122 132 152 177
35 48 53 61 81 108 125 131 153 174
36 49 55 66 95 102 125 133 165 177
31 47 57 63 94 109 129 136 164 179
35 47 51 72 98 108 128 135 162 175
36 43 51 74 94 104 129 139 169 175
32 46 53 74 95 107 127 144 164 173
38 48 55 78 97 105 124 145 168 171
39 44 59 77 98 108 129 147 166 172
As you can see, each time the length of fread output is either equal to or less than the buffer size. But it only jumps to the next column when the current one has been completely received.
I am trying to compare multiple image using corr2 to see the similarity in correlation.
for i=1:2
first_img = imread(sprintf('%g.jpg',i));
first_size = size(first_img);
size_temp = size(first_size);
max_size = max(size_temp);
if max_size == 3
first_img = rgb2gray(first_img);
first_size = size(first_img);
end
for j=i+1:2
second_img = imread(sprintf('%g.jpg',j));
second_size = size(second_img);
size_temp = size(second_size);
max_size = max(size_temp);
if max_size == 3
second_img = rgb2gray(second_img);
second_size = size(second_img);
end
if i == j
continue;end
if first_size ~= second_size
continue;end
if first_size == second_size
correlation_fs = corr2(first_img,second_img);
if correlation_fs == 1
fprintf('%g is the same as %g\n',first_img,second_img);
end
end
end
end
now, the problem show up when the first image compared to the 3rd dummy image which is exactly the same as the first image.
219 is the same as 219
220 is the same as 220
221 is the same as 221
221 is the same as 222
224 is the same as 223
222 is the same as 221
221 is the same as 222
223 is the same as 224
218 is the same as 236
242 is the same as 232
217 is the same as 219
226 is the same as 228
220 is the same as 229
241 is the same as 251
254 is the same as 253
250 is the same as 247
253 is the same as 253
252 is the same as 248
237 is the same as 224
217 is the same as 218
225 is the same as 219
219 is the same as 223
219 is the same as 214
222 is the same as 237
I don't know why this is showing up, it should print that image 1 is the same as image 3, at least is what i want it to.
You are printing out the entire image matrices instead of the image number. Try:
fprintf('%g is the same as %g\n',i,j)
Think about what first_img is, it's a matrix of pixel intensities. So you're printing out all the pixel values.
fprintf('%g is the same as %g\n',first_img,second_img);
Here you are passing two images as arguments. You should have passed the image numbers instead.
fprintf('%g is the same as %g\n', i, j);