wait bar in MATLAB gui in nested FOR loop
显示 更早的评论
I have to show a progress bar in matlab guide that appears while running FOR loop and shows progress for each iteration. Moreover, the user should not be able to close it until the calculation stops, so as to prevent the user from making any changes. Example code is as follows
MBTs = minMBT:stepMBT:maxMBT;
MATs = minMAT:stepMAT:maxMAT;
i=1;
for MAT = MATs
j = 1;
for MBT = MBTs
%some calculations
j = j + 1;
end
i = i + 1;
end
采纳的回答
MBTs = minMBT:stepMBT:maxMBT;
MATs = minMAT:stepMAT:maxMAT;
n = length(MATs) * length(MBTs);
c = 0;
H = waitbar(0, 'Please wait...');
for i1 = 1:length(MATs)
MAT = MATs(i1);
for i2 = 1:length(MBTs)
MBT = MBTs(i2);
%some calculations
% Update the waitar:
if ~ishghandle(H)
error('User closed waitbar.');
end
c = c + 1;
waitbar(c / n, H, sprintf('%d of %d', c, n));
end
end
It would be brute to forbid, that the user closes the waitbar. Imagine that the loop runs for 2 hours for unknown reasons. Do you really block a premature stopping? User hate such restrictions for good reasons.
9 个评论
thanks Jan for such a detailed answer. I actually want to disable the cancel function so that no changes can be made by the user during the execution otherwise it will become mess
There is one more thing that I want to change the arrow cursor to hour watch (loading cursor) and it should change back to the arrow curser after loof stops.
What does "disable the cancel function" mean? Which cancel function? If you do not specify the 'CreateCancelBtn' property of waitbar, there is no Cancel button.
H = waitbar(0, 'Please wait...');
WindowAPI(H, 'Button', 'off');
I've showed in my code how to catch a closed waitbar window. You should be able to start a proper cleanup instead of the error.
To change the cursor:
H = waitbar(0, 'Please wait...');
set(H, 'Pointer', 'watch');
...
set(H, 'Pointer', 'arrow');
I have tried doing this for cursor but it only changes cursor shape on the window where the waitbar is showing, What if I want to chnage it on entire GUI?
What is "the entire GUI"? Matlab's command window or the operating system? You can set the cursor in each window by using its handle. So if you have e.g. a figure created by GUIDE, use its handle for the above commands.
I mean Matlabs uipanel where the buttons are present. I am attaching the figure the cursor is curently chnaging when i put it on waitbar not the window below it
So you mean the window called "Untitled"? It seems to be created by GUIDE. And I guess, that the waitbar is opened inside one of the callbacks? Then insert there:
FigH = ancestor(hObject, 'figure');
set(FigH, 'Pointer', 'watch');
...
set(FigH, 'Pointer', 'arrow');
You have the figure handle inside the struct handles also, but I'm not sure, how it is called. You can check this by your own: Set a breakpoint in the code and display the contents of the handles struct.
Thank you so much for the help Jan.
You are welcome, Aash.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
标签
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
