Note that there are some explanatory texts on larger screens.

plurals
  1. POMatlab GUI: referencing an existing object handle using a variable
    primarykey
    data
    text
    <p>I am currently working on my matlab final project for school. I consider myself fairly knowledgeable and proficient when it comes to programming.. but Matlab simply has TOO many oddities.</p> <blockquote> <p><strong><em>The fundamental question</em></strong> <em>(realized this after finding the answer)!</em> How can I use a variable in a call to a gui handle object without the name of the variable being used instead of the value? </p> <p><em>In other words:</em> <strong>Use a variable in a field name</strong> (If I had known it was this simple I wouldn't of asked)</p> </blockquote> <p><em>My project</em> is building a simple rendition of the age old 'Battleship' game.</p> <p><strong>My issue</strong>: I currently 5 objects (axes) for the ship pieces. They are selected one at a time to be moved into another location(the grid). <em>I am able to</em> use the <strong>setpixelposition</strong> to move each object after a <strong>button</strong> click. </p> <p>Right now under the button click, I have something like this</p> <pre><code>function btnPlaceShip_Callback(hObject, eventdata, handles) %Store the current selected ship(passed from an onclick to a label) ship = get(handles.lblSelectedShip,'string'); %I have tried everything I could think of, but basically I want to achieve the %following setpixelposition(handles.ship, [0 250 50 250]) %where the variable 'ship' contains the name of the object. </code></pre> <p>In other words, the <em>var</em> <strong>ship</strong> = 'shipAircraftCarrier', and..</p> <pre><code> setpixelposition(handles.shipAircraftCarrier, [0 250 50 250]) </code></pre> <p><strong>works!</strong>(sets the position of the specific ship indicated). Using the variable <strong>ship</strong>, however, matlab takes the string literally and not for its value. It would be extremely convenient to use the variable instead!</p> <p>If anyone have any solutions, I would be grateful. I have scoured the web but perhaps I am missing some fundamental understanding of the Matlab GUI stuff - The matlab help documents are very non-descriptive and are not of much help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload