I have created GUI:
  • ListBox created, and Move up button in matlab as '.m' it is working
Code:
function moveUp(src,event,lbox)
index_move = ismember(1:length(lbox.Items),lbox.Value);
index_shift = circshift(index_move,-1);
position = 1:length(lbox.Items);
if isequal(lbox.Value,1)
position = circshift(position,-1);
else
value_up = position(index_move);
value_down = position(index_shift);
position(index_shift) = value_up;
position(index_move) = value_down;
end
set(lbox,'Items',lbox.Items(position));
set(lbox,'Value',find(index_shift));
end
  • But the same code is not working in Matlab app designer
Code:
index_move = ismember(1:length(app.ListBox.Items), size(app.ListBox.Value));
index_shift = circshift(index_move,-1);
position = 1:length(app.ListBox.Items);
if isequal(app.ListBox.Value,1)
position = circshift(position,-1);
else
value_up = position(index_move);
value_down = position(index_shift);
position(index_shift) = value_up;
position(index_move) = value_down;
end
set(app.ListBox.Items,app.ListBox.Items(position));
set(app.ListBox.Value,find(index_shift));
Kindly suggest a way for this?

 채택된 답변

Kevin Holly
Kevin Holly 2022년 7월 7일

0 개 추천

The following code worked as a callback function for me.
index_move = ismember(app.ListBox.Items, app.ListBox.Value);
index_shift = circshift(index_move,-1);
position = 1:length(app.ListBox.Items);
if isequal(app.ListBox.Value,1)
position = circshift(position,-1);
else
value_up = position(index_move);
value_down = position(index_shift);
position(index_shift) = value_up;
position(index_move) = value_down;
end
set(app.ListBox,'Items',app.ListBox.Items(position));
set(app.ListBox,'Value',app.ListBox.Items(find(index_shift)));

댓글 수: 10

Kevin Holly
Kevin Holly 2022년 7월 7일
Please see app attached.
Kevin Holly
Kevin Holly 2022년 7월 7일
편집: Kevin Holly 2022년 7월 7일
Change the List Box Value to Program1 and clear ItemsData, then it should work.
You don't need the updateorder function.
You can just do the following:
function SaveChangesButtonPushed(app, event)
app.callingapp.ListBox.Items = app.ListBox.Items;
app.callingapp.ListBox.Value = app.ListBox.Value;
delete(app);
end
Would you rather change the positions without the extra window? It is possible to change the order with the ItemsData still intact, but the approach would be different.
Lavanya
Lavanya 2022년 7월 8일
Ok, How to do that with ItemData? Because there are many features linked to this.
For example : 'AddButton' - Folderadded
'ListBox' - Foldername show in the list box
'Panel' - .dat files related to folder appear'
like that so many features added and linked (I can't change the ItemData)
If possible Can you send the file with positions with 'ItemsData'?
For the "Copy_of_ListBoxdialogapp.mlapp" that you attached earlier, you can do the following:
function movedownButtonPushed(app, event)
index_move = ismember(app.ListBox.Items, app.ListBox.Items(str2num(app.ListBox.Value)));
index_shift = circshift(index_move,1);
position = 1:length(app.ListBox.Items);
if isequal(str2num(app.ListBox.Value),1)
position = circshift(position,1);
else
value_up = position(index_move);
value_down = position(index_shift);
position(index_shift) = value_up;
position(index_move) = value_down;
end
set(app.ListBox,'Items',app.ListBox.Items(position));
set(app.ListBox,'Value',num2str(find(index_shift)));
end
Lavanya
Lavanya 2022년 7월 11일
편집: Lavanya 2022년 7월 11일
Thankyou code works fine in "Copy_of_ListBoxdialogapp.mlapp".
But It is not working in -Main GUI. It shows the following error as ''2.JPG''.
I tried to chnage ''str2num -> str2double'' . then I am getting error as ''1.JPG''
Lavanya
Lavanya 2022년 7월 12일
Is it not possible?
Kevin Holly
Kevin Holly 2022년 7월 12일
In your app, is app.ListBox.Value a numeric value?
Lavanya
Lavanya 2022년 7월 13일
편집: Lavanya 2022년 7월 13일
When we click the listbox, It has data i.e Image data & setup data in panel(read from '.dat ' file)
I have this function in main app and I wanted to execute 'move up' and 'move down ' operations in dialogapp. How to execute this function in 'dialogapp'? In this 'setup_' should update when move up and move down operations used
function updateRunOrder(app,orderconnection,newnames)
% update the order of all ListBoxes
temp_Items = app.ListBox.Items; % all List Boxes schould have the same items
for i=1:length(orderconnection)
% reorder:
app.ListBox.Items{i} = temp_Items{orderconnection(i)};
app.ListBoxGeometry.Items{i} = temp_Items{orderconnection(i)};
% rename:
app.ListBox.Items{i} = newnames{i};
app.ListBoxGeometry.Items{i} = newnames{i};
% change setup assignment:
setup_temp(i) = app.(strcat('setup_', string(i))); % save old setups
end
for i=1:length(orderconnection)
app.(strcat('setup_', string(i))) = setup_temp(orderconnection(i)); %assign saved, old setups to new order
end
% select the stup that is currently shown:
lastval = str2double(app.ListBox.Value{1}); % last shown setup
app.ListBox.Value{1} = num2str(orderconnection(lastval));
app.ListBoxGeometry.Value{1} = num2str(orderconnection(lastval));
end
Kevin Holly
Kevin Holly 2022년 7월 14일
If you are using the function in both apps, I would suggest saving the function as a file that is called from each app. You can change the input to the function based on what app you call from (e.g. updateRunOrder(app,orderconnection,newnames) or updateRunOrder(app.Callingapp,orderconnection,newnames)).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2022년 7월 7일

댓글:

2022년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by