Trouble applying correctly the ListBoxTop property of uicontrol

3 次查看(过去 30 天)
Hello,
I'm trying to list some status messages (which I already display in the command window) in a ListBox UIControl. I'm using 7.12.0 (R2011a) version of Matlab. Here is my code :
function Main
clc;
close all;
clear all;
global Window;
global ControlHandle;
persistent NumberOfCall;
if not(exist('NumberOfCall','var')) || isempty(NumberOfCall)
NumberOfCall = 1;
end
AddWindow(80,100,[1,1,1]);
ControlHandle = uicontrol('Parent',Window.Handle,'Position',[10,10,Window.Width-20,Window.Height/2-20],'Style','ListBox','ListBoxTop',NumberOfCall,...
'FontSize',10,'FontWeight','Normal','FontName','FixedWidth','String',[]);
NewMessage = sprintf(strcat(32,'Plotting data started on',32,datestr(now,'dddd'),',',32,'the',32,datestr(now,'dd mmmm yyyy'),32,'at',32,datestr(now,'HH:MM:SS.FFF'),'\n'));
disp(' ');
disp(NewMessage);
Status = regexp(NewMessage,'\n','split');
set(ControlHandle,'String',Status);
set(ControlHandle,'ListBoxTop',NumberOfCall);
for k = 1:150
NewMessage = sprintf(strcat(NewMessage,'\n',32,32,32,'Getting closer to the solution ...','\n'));
disp(NewMessage);
Status = regexp(NewMessage,'\n','split');
set(ControlHandle,'String',Status);
set(ControlHandle,'ListBoxTop',NumberOfCall);
end
NumberOfCall = NumberOfCall+1;
NumberOfMessages = NumberOfCall;
end
function NumberOfWindows = AddWindow(MarginWidth,MarginHeight,Color)
global Window;
persistent NumberOfCall;
if not(exist('NumberOfCall','var')) || isempty(NumberOfCall)
NumberOfCall = 0;
end
Window.MarginWidth = MarginWidth;
Window.MarginHeight = MarginHeight;
Window.Color = Color;
% Creating the main window
SizeScreen = get(0,'ScreenSize');
Window.Width = SizeScreen(3)-MarginWidth;
HorizontalPosition = Window.MarginWidth/2;
Window.Height = SizeScreen(4)-Window.MarginHeight;
VerticalPosition = Window.MarginHeight/4;
Window.Handle = figure;
set(gcf,'NumberTitle','Off');
set(gcf,'Name','Testing the status box...');
set(gcf,'MenuBar','None');
set(gcf,'DoubleBuffer','On');
set(gcf,'Position',[HorizontalPosition VerticalPosition Window.Width Window.Height]);
set(gcf,'Color',Window.Color);
NumberOfCall = NumberOfCall+1;
NumberOfWindows = NumberOfCall;
end
It works fine excepted for the following command I looked up in the documentation and into this post :
set(ControlHandle,'ListBoxTop',NumberOfCall);
Do someone have any idea why? Thank you very much !
Csaba

回答(6 个)

Jan
Jan 2011-10-20
This line can contain two different errors:
set(ControlHandle,'ListBoxTop',NumberOfCall);
  1. ControlHandle is not defined or not a UICONTROL('listbox') handle.
  2. The ListBoxTop value is out of range: 1 <= Value <= number of list entries.
The error message reveals, which of these two problems occurred. Therefore it is always a good idea to post the complete message in this forum.

Csaba
Csaba 2011-10-20
Thank you for your answer.
Sadly, the line you mentionned can certainly contain more than the errors you've listed, because I get no error message.
And the handle is the right one (at least the one of the ListBox uicontrol, because I'm able to add a string as message to the ListBox uicontrol that I'm using as a message box in a panel.
The first line of the ListBox uicontrol is highlighted in blue, what I'm interpreting as a sign that ListBoxTop sets the Top line to 1. But I can't explain why, because I've checked and NumberOfCall get the right values (1 then 2 then 3 and so on as long as strings are added to the vector of string set by the following command :
set(ControlHandle,'String',Status);
Thank you in advance for some other ideas, Csaba

Robert Cumming
Robert Cumming 2011-10-20
how many items are in your listbox?
Does your listbox have a vertical scrollbar?
If you dont have enough items in the list to require a scrollbar the 'listboxtop' property wont do anything

Csaba
Csaba 2011-10-21
I have like 150 items and I also do have a scrollbar which works manually, but the top line in my listbox is the first one, no matter what.
: (

Jan
Jan 2011-10-21
I do not understand, why you are setting the ListBoxTop value 150 times in the "for k = 1:150" loop to the same value.
You store the value of NumberOfCall persistently and increase it in every call. But then you use a clear all to remove all loaded function from the memory - together with their persistent variables. The next time your Main function is called, the persistent variable is created again and initialized to 1. The same happens in AddWindow.
Do you have any reason to use the brutal three sisters:
clc;
close all;
clear all;
??? While clc and close all is a question of taste, clear all is only useful if you have modified all M-files in the path dynamically during the program runs. Perhaps you want a clear variables, but this is not useful inside a function also.
I suggest another time to avoid the clear all, see: good programming praxis: clear all.

Csaba
Csaba 2011-11-25
You're absolutely right about the bad praxis and the use of clear all; but it's the simplest and probably not the best way I found to solve a previous problem. Moreover, the code you find above does the job excepted my little focusing problem, because I call the Main function only once.
The reason why I call ListboxTop 150 times (i.e. every time NumberOfCall is incremented) is because I want the last line of the Listbox uicontrol to be visible.
So the only problem which remains is this bad focusing and probably me missusing the ListBoxTop property... Any other ideas?
Thanks, Csaba
  1 个评论
Robert Cumming
Robert Cumming 2011-11-28
You are trying to use a persistent variable to store the NumberOfCall - but any memory held by that variable is cleared when you do "clear all" (which as you've acknowledge you shouldn't use here.
The NumberOfCall is not incremented in your loop.
Try putting
set(ControlHandle,'ListBoxTop',k);
and see what happens - its important you understand why that will give you a different result - it will also help you understand what was going wrong previously.
Remember to use the debugger and step through to see what the variables are.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by