Waitbar does not increment

조회 수: 8 (최근 30일)
Richard
Richard 2011년 12월 10일
답변: Daniele Finazzi 2022년 10월 24일
I am trying to use waitbar to show the progress of a for loop that is stepping through a data file using memmapfile and doing array arithmetic on the data. The bar remains at 0 until the loop exits and then jumps to full scale in one jump. I have tried adding a refresh or a drawnow following the waitbar update with no success. I have run the approxpi example from help and that runs correctly. The loop, which works correctly, looks like this: (somewhat simplified, leaving out the array definitions and the full table structure)
Previous_Frequency = 0;
Current_Frequency = 0;
Data_Offset = PCT_Count + (4 * Echo_Count); % total size of pulse table and data block in bytes
hwb = waitbar(0, 'Getting the data, Please wait');
j = ones(1, 1,'int32'); % new frequency counter
for p = 1 : Total_pulse_Count
waitbar(p / Total_pulse_Count, hwb);
Byte_Offset = PCT_Offset + ( (p - 1) * Data_Offset);
PCT = memmapfile (Full_Name, 'Offset', Byte_Offset, 'Repeat', 1, 'Format', {
'int32' [1 1] 'record_id' ;
%etc etc
}); % Format and object end. Length 144 bytes
PCT_Data = memmapfile (Full_Name, 'Offset', Byte_Offset + PCT_Count, 'Repeat', 1, 'Format', {
'int16' [2 eval(num2str(Num_Receivers)) eval(num2str(Num_Gates))] 'IQRxRG'
}); % Format and object end.
TempIQ = PCT_Data.Data(1).IQRxRG; % read the data into memory for faster access
TempIQ = swapbytes(TempIQ);
Current_Frequency = PCT.data.frequency;
if p == 1 % special case, first loop
Previous_Frequency = Current_Frequency;
Frequency_Scale(j, 1) = PCT.data.frequency;
end
if Current_Frequency ~= Previous_Frequency % increment the new frequency counter, update Frequency Scale
j = j + 1;
if j > Frequency_Count, break, end
Frequency_Scale(j, 1) = PCT.data.frequency;
end;
for m = 1 : Num_Receivers;
for k = 1 : Num_Gates % integrate each signal and calculate the magnitude for each antenna
if Current_Frequency ~= Previous_Frequency % start a new integration
Plot_Data_Ant(k, j - 1, m) = (I(k, m)^2 + Q(k, m)^2)^0.5; % Write the earlier loop data
I(k, m) = (TempIQ(1,m,k));
Q(k, m) = (TempIQ(2,m,k));
IT(k, j) = IT(k, j) + I(k, m);
QT(k, j) = QT(k, j) + Q(k, m);
else
I(k, m) = I(k, m) + (TempIQ(1,m,k));
Q(k, m) = Q(k, m) + (TempIQ(2,m,k));
IT(k, j) = IT(k, j) + (TempIQ(1,m,k));
QT(k, j) = QT(k, j) + (TempIQ(2,m,k));
end
end
end
for k = 1 : Num_Gates % calculate the magnitude for all the antennas
Plot_Data_All(k, j) = (IT(k, j)^2 + QT(k, j)^2)^0.5;
end
Previous_Frequency = Current_Frequency;
end;
Plot_Data_Ant(k, j, m) = (I(k, m)^2 + Q(k, m)^2)^0.5;
close(hwb);
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 10일
Why are you using
[2 eval(num2str(Num_Receivers)) eval(num2str(Num_Gates))]
instead of the much more straight-forward
[2 Num_Receivers Num_Gates]
??

댓글을 달려면 로그인하십시오.

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 12월 10일
Inside the for-loop, can you try adding either "drawnow" or "pause(0.2)" after the waitbar() statement?
I never had problem regarding updating the waitbar figure. Maybe you can comment out some of the code inside the loop to see what happens.
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 12월 11일
Ah... I just remembered -- doesn't waitbar() have a rate limiter built in, so that it only updates every second or two? Stores the elapsed time as a property of the waitbar patch, and only updates the patch if enough time has passed ?
If my memory is correct, then waitbar() would not increment for any sufficiently fast (total time) loop.
Fangjun Jiang
Fangjun Jiang 2011년 12월 11일
I don't see a rate limit. It definitely can update much faster than every second. I've had waitbar progress continuously as fast as you pour coffee to a cup.
On the other side, it can not be due to that the OP's loop is too fast. Run the bare waitbar() below will see a nice progress.
%%
h=waitbar(0,'progress');
for k=1:1000
waitbar(k/1000,h);
end

댓글을 달려면 로그인하십시오.


Daniele Finazzi
Daniele Finazzi 2022년 10월 24일
This question is quite old, but since I have recently had the same issue, here is how I fixed it.
My guess is that Matlab performs an integer division in the first argument of the waitbar, which then results in either 0 (0%) or 1 (100%). In my case, I displayed the percent progress on the waitbar like this (using the code above as an example):
waitbar(p / Total_pulse_Count, hwb, sprintf('%d%%', p*100/Total_pulse_Count));
and I noticed the bar was on 0 until 50% was reached, and after that it jumped straight to "full scale".
So I just forced the division inside the waitbar function to be non-integer:
waitbar(double(p) / double(Total_pulse_Count), hwb);
What I don't know is why this happens in some cases only. Up until now I had been using the waitbar in the same way without ever running into this issue.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by