Trouble applying correctly the ListBoxTop property of uicontrol
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
1 Kommentar
Antworten (6)
Jan
am 20 Okt. 2011
This line can contain two different errors:
set(ControlHandle,'ListBoxTop',NumberOfCall);
- ControlHandle is not defined or not a UICONTROL('listbox') handle.
- 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.
0 Kommentare
Robert Cumming
am 20 Okt. 2011
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
0 Kommentare
Jan
am 21 Okt. 2011
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.
0 Kommentare
Csaba
am 25 Nov. 2011
1 Kommentar
Robert Cumming
am 28 Nov. 2011
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.
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!