Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:58:59+00:00 2026-05-23T13:58:59+00:00

I wrote a script that returns several text boxes in a figure. The text

  • 0

I wrote a script that returns several text boxes in a figure. The text boxes are moveable (I can drag and drop them), and their positions are predetermined by the data in an input matrix (the data from the input matrix is applied to the respective positions of the boxes by nested for loop). I want to create a matrix which is initially a copy of the input matrix, but is UPDATED as I change the positions of the boxes by dragging them around. How would I update their positions? Here’s the entire script

function drag_drop=drag_drop(tsinput,infoinput)
[x,~]=size(tsinput);
dragging = [];
orPos = [];
fig = figure('Name','Docker Tool','WindowButtonUpFcn',@dropObject,...
'units','centimeters','WindowButtonMotionFcn',@moveObject,...
'OuterPosition',[0 0 25 30]);
% Setting variables to zero for the loop
plat_qty=0;
time_qty=0;
k=0;
a=0; 
% Start loop
z=1:2
    for idx=1:x
        if tsinput(idx,4)==1
            color='red';
        else
            color='blue';
        end
        a=tsinput(idx,z);
       b=a/100;
        c=floor(b); % hours
        d=c*100;
        e=a-d; % minutes
        time=c*60+e; % time quantity to be used in 'position'
        time_qty=time/15;
        plat_qty=tsinput(idx,3)*2;
        box=annotation('textbox','units','centimeters','position',...
        [time_qty plat_qty 1.5 1.5],'String',infoinput(idx,z),...
        'ButtonDownFcn',@dragObject,'BackgroundColor',color); 
        % need to new=get(box,'Position'), fill out matrix OUT of loop
    end
    fillmenu=uicontextmenu;
    hcb1 = 'set(gco, ''BackgroundColor'', ''red'')';
    hcb2 = 'set(gco, ''BackgroundColor'', ''blue'')';
    item1 = uimenu(fillmenu, 'Label', 'Train Full', 'Callback', hcb1);
    item2 = uimenu(fillmenu, 'Label', 'Train Empty', 'Callback', hcb2);
    hbox=findall(fig,'Type','hggroup');
    for jdx=1:x
        set(hbox(jdx),'uicontextmenu',fillmenu);
    end
end
new_arr=tsinput;

function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end

function dropObject(hObject,eventdata,box)
if ~isempty(dragging)
    newPos = get(gcf,'CurrentPoint');
    posDiff = newPos - orPos;
    set(dragging,'Position',get(dragging,'Position') + ...
    [posDiff(1:2) 0 0]);
    dragging = [];
end
end

function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end

% Testing purpose input matrices:
% tsinput=[0345 0405 1 1 ; 0230 0300 2 0; 0540 0635 3 1; 0745 0800 4 1]
% infoinput={'AJ35 NOT' 'KL21 MAN' 'XPRES'; 'ZW31 MAN' 'KM37 NEW' 'VISTA';
% 'BC38 BIR' 'QU54 LON' 'XPRES'; 'XZ89 LEC' 'DE34 MSF' 'DERP'}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:58:59+00:00Added an answer on May 23, 2026 at 1:58 pm

    If I understand you correctly (and please post some code if I’m not), then all you need is indeed a set/get combination.

    If boxHandle is a handle to the text-box object, then you get its current position by:

    pos = get (boxHandle, 'position')
    

    where pos is the output array of [x, y, width, height].
    In order to set to a new position, you use:

    set (boxHandle, 'position', newPos)
    

    where newPos is the array of desired position (with the same structure as pos).

    EDIT

    Regarding to updating your matrix, since you have the handle of the object you move, you actually DO have access to the specific text box.

    When you create each text box, set a property called ‘UserData’ with the associated indices of tsinput used for that box. In your nested for loop add this

    set (box, 'UserData', [idx, z]);
    

    after the box is created, and in your moveObject callback get the data by

    udata = get(dragging,'UserData');
    

    Then udata contains the indices of the elements you want to update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote a small FQL script that returns the latest wallposts of my fanpage.
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I wrote a Python script that does some task to generate, and then keep
This is a crude korn shell script that someone else wrote. I don't know
I have a Perl script I wrote for my own personal use that fetches
I wrote a short bash script to complete a task that involves creating a
I have a Python script I recently wrote that I call using the command
I got a JavaScript code I wrote that calls a web service returns some
I'm essentially writing a little script that needs to save several different files as
I wrote a function which runs a php script and returns its output. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.