Reversing parts of (even) items in a listbox (cellarray)

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.
items=app.ListBoxPos.Items
nl=numel(items);
rstr=["r2","r4","r6","r8"];
s1=[];
out={};
for i=1:nl
line=char(items(i));
if contains(line, rstr)
tokens = strsplit(line, 'r' )
t=fliplr(tokens{1,1})
new=[t,' r',tokens{1,2}]
app.ListBoxPos.Items{i}=new
%Not sure what to do now!
end
end
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
Jason

 Respuesta aceptada

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.
items=app.ListBoxPos.Items
nl=numel(items);
for i=1:nl
if mod(items{i}(end),2)==0
out{i}=items{i}([end-3:-1:1 end-2:end]);
else
out(i)=items(i);
end
end
app.ListBoxPos.Items = out;

8 comentarios

Jason
Jason el 18 de En. de 2021
Hi, thanks for this. Im getting all rows satisfying the condition
if mod(items{i}(end),2)==0
How do you populate your listbox? This is how I tested it.
fig = uifigure;
ListBoxPos = uilistbox(fig,'Items',{'1 2 r1','1 2 3 r2','1 2 r3','1 2 3 r4'});
Save your variable items to a mat file and attach it to your post using the paperclip icon.
Jason
Jason el 18 de En. de 2021
I found if I do
L=str2num(items{i}(end) Then do If mod(L,2)==0
It works. (Sorry for not formatting in code as Im using a phone)
Cris LaPierre
Cris LaPierre el 19 de En. de 2021
Editada: Cris LaPierre el 19 de En. de 2021
This update will handle double digit numbers at the end (e.g. 'r10' and 'r12').
r = strfind(items{i},' r');
out{i}=items{i}([r-1:-1:1 r:end]);
Hi, do you mean use that instead of
out{i}=items{i}([end-3:-1:1 end-2:end]);
Jason
Jason el 19 de En. de 2021
Editada: Jason el 19 de En. de 2021
This seems to work (have to convert to integers first
item=items{i};
n=item(end)
token = strtok(item,'r');
tn=str2num(token);
t=fliplr(tn);
out{i}=[num2str(t),' r',n]
Cris LaPierre
Cris LaPierre el 19 de En. de 2021
Editada: Cris LaPierre el 19 de En. de 2021
What version of MATLAB are you using?
It's good to see you coming up with your own approach. Nice job. Just be aware that this solution suffers from the original problem - it doesn't work when the final number has 2 digits (10 or 12).
Jason
Jason el 19 de En. de 2021
Im using 2020b

Iniciar sesión para comentar.

Más respuestas (1)

Jason
Jason el 19 de En. de 2021
Editada: Jason el 19 de En. de 2021
Here is my solution based on Cris' answer:
val = app.SNAKESCANCheckBox.Value;
switch val
case 1
items=app.ListBoxPos.Items;
nl=numel(items);
out={};
for i=1:nl
L=str2num(items{i}(end));
if mod(L,2)==0
out{i}=items{i}([end-3:-1:1 end-2:end]);
else
out{i}=items{i};
end
end
app.ListBoxPos.Items=out;
case 0
end
I thought this worked, but when I have a number 10,11 or 12 (my numbers can be 1-12), then it doesn't quite work - Its reversing the order of the double digits

1 comentario

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'.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 18 de En. de 2021

Editada:

el 19 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by