Why Does My Listbox Output Keep Replacing the Previous One in Matlab?

  • Thread starter jaclyn89
  • Start date
  • Tags
    Output
In summary, the conversation discusses a problem with displaying multiple outputs in a listbox using a for loop in Matlab. The goal is to display the names of images with an average value of less than or equal to 10, but the current output keeps replacing the previous output. The conversation also includes a snippet of code and a question about the programming language being used.
  • #1
jaclyn89
5
0
hi, i am facing some problem when i want to display several output in a listbox.

i have a for loop to load the images from a folder, after the images loaded, it will gone through some calculation to get the average. if the images average <=10, then i want to display the images name in a listbox. when i have 5 images' average<=10, then i should display 5 images name in the list box. but now my problem is, the current output will keep replace the previous output.

anyone can help me? here is some part of my code:

sdirectory = 'D:\recognition system\database'; %specific the file directory
filess = dir([sdirectory '/*.png']);
for k = 1:length(filess)
filename = [sdirectory '\' filess(k).name];
I = imread(filename);

...
aveg(k)=abs((standev/mean1)*100);

if( aveg(k)<= 10 )


newText=sprintf('the percentage of diffrence for <<%s>> is %0.0f \n ',filename,aveg(k));
set(handles.listbox1,'String',newText);

else

set(handles.listbox1,'String','');
end
end
 
Physics news on Phys.org
  • #2
well first question is what programming language are you using? is this matlab?
 
  • #3
yes, i am using matlab...
 
  • #4
anyone please...
 
  • #5


One possible solution to your problem is to use the "add" function for the listbox instead of the "set" function. This will add the new output to the list instead of replacing the previous output. Your code could look something like this:

sdirectory = 'D:\recognition system\database'; %specific the file directory
filess = dir([sdirectory '/*.png']);
for k = 1:length(filess)
filename = [sdirectory '\' filess(k).name];
I = imread(filename);

...
aveg(k)=abs((standev/mean1)*100);

if( aveg(k)<= 10 )
newText=sprintf('the percentage of diffrence for <<%s>> is %0.0f \n ',filename,aveg(k));
% use "add" function to add new output to the list
add(handles.listbox1, newText);
else
% use "add" function to add blank line to list
add(handles.listbox1, '');
end
end

Alternatively, you could also store the output in a cell array and then use the "set" function to display the entire array in the listbox. This way, each new output will be added to the existing array instead of replacing it. Your code could look something like this:

sdirectory = 'D:\recognition system\database'; %specific the file directory
filess = dir([sdirectory '/*.png']);
output = {}; % initialize empty cell array
for k = 1:length(filess)
filename = [sdirectory '\' filess(k).name];
I = imread(filename);

...
aveg(k)=abs((standev/mean1)*100);

if( aveg(k)<= 10 )
newText=sprintf('the percentage of diffrence for <<%s>> is %0.0f \n ',filename,aveg(k));
% add new output to cell array
output{end+1} = newText;
else
% add blank line to cell array
output{end+1} = '';
end
end
% display entire cell array in listbox using "set" function
set(handles.listbox1, 'String', output);
 

FAQ: Why Does My Listbox Output Keep Replacing the Previous One in Matlab?

How do I display output in a listbox?

To display output in a listbox, you can use the listbox element in HTML or the ListBox class in programming languages like C# or Java. You will need to add items to the listbox and specify the display properties for each item.

What is the purpose of using a listbox to display output?

A listbox is a useful tool for displaying a list of items in a compact and organized way. It allows users to easily select and view multiple items at once, making it ideal for displaying large amounts of data or options for selection.

How do I add items to a listbox?

To add items to a listbox, you can use the addItem() or add() method in programming languages like JavaScript or C#. You can also manually add items using HTML by using the option element inside the listbox tags.

Can I customize the appearance of a listbox?

Yes, you can customize the appearance of a listbox by using CSS or by setting properties such as font size, color, and spacing. You can also use templates or data binding to dynamically change the appearance of the items in the listbox.

How do I retrieve selected items from a listbox?

To retrieve selected items from a listbox, you can use the getSelectedItems() method in programming languages like C# or selectedOptions property in JavaScript. You will need to loop through the selected items to access their values or properties.

Similar threads

Replies
4
Views
4K
Back
Top