Reversing parts of (even) items in a listbox (cellarray)
1 view (last 30 days)
Show older comments
Jason on 18 Jan 2021
Edited: Cris LaPierre on 19 Jan 2021
Hello, I have items in a listbox (so cell array) - the integers before the "r" can be any number from 1-12.
My aim is to take any line containing an even number after r (r2, r4 etc) and reverse the other numbers and then put back into the list box
So line 2 & 4 would change to
1 2 3 r2 -> 3 2 1 r2
1 2 3 r4 -> 3 2 1 r4
This was my attempt.
if contains(line, rstr)
tokens = strsplit(line, 'r' )
%Not sure what to do now!
Not sure if this is the most elegant way, and it falls over trying to overwrite the line in the listbox.
Thanks for any help
Cris LaPierre on 18 Jan 2021
You can use mod to determine if the last number is even or not. I would then use indexing to reverse the order of the rows that end in an even number.
app.ListBoxPos.Items = out;
More Answers (1)
Edited: Jason on 19 Jan 2021
Cris LaPierre on 19 Jan 2021
When the number at the end is 2 characters, it would require a different approach. Perhaps finding the 'r' and using that to set the index rather the 'end'.
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!