MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

       

In this article, you will learn how to remove redundant items from a list.

I want to share this function because, although it’s not the most complex function to write, I’ve used this function for a long time and made it more robust over the years. 

This is a key function to me for identifying Simulink paths containing blocks with a specific configuration. For example, if I want to document the location of the subsystems containing constant blocks set to “Inherit: Inherit via back propagation,” every subsystem containing more than one constant block set to this configuration will appear more than once. Removing these redundancies may prevent you from getting lost among these paths:

function newList = deleteListRedundancies(list)
 
newList = [];
for i = 1:length(list)
    nameLoop1 = list{i};
    addNameLoop1 = 1;
    for j = (i+1):length(list)
        nameLoop2 = list{j};
        if (strcmp(nameLoop1, nameLoop2) > 0)
            addNameLoop1 = 0;
        end
    end
    if addNameLoop1
        newList = [newList ; {nameLoop1}];
    end
end
 
return

Here is an example of how to use this function: 

list = [
    {'this/is/my/first/path'} ; ...
    {'this/is/my/first/path'} ; ...
    {'this/is/my/first/path'} ; ...
    {'this/is/my/second/path'} ; ...
    {'this/is/my/third/path'} ; ...
    {'this/is/my/fourth/path'} ; ...
    {'this/is/my/fourth/path'} ; ...
    {'this/is/my/fourth/path'} ; ...
    {'this/is/my/fifth/path'} ; ...
    {'this/is/my/fifth/path'} ; ...
    {'this/is/my/fifth/path'} ; ...
    ];
 
newList = deleteListRedundancy(list)

After running this script, we get:


Leave a Reply