This forum is in permanent archive mode. Our new active community can be found here.

Weekend coding



  • What if you set the C++ pointers to NULL immediately after deleting them in the destructors (not a MATLAB expert, so I'm not sure if you can do this with what you're doing)? The C++ standard says that deleting a NULL pointer is a no-op and always safe.
  • edited March 2012
    That's pretty weird. Still, all you would have to do is nullify the in-MATLAB C++ object reference in your MATLAB destructor, and only call the C++ destructor if the C++ reference hasn't been nullified.
    EDIT: Yup, Dragonmaster Lou and I had a similar thought.
    Post edited by lackofcheese on
  • Yeah, I did more debugging and it appears that the deconstructor isn't even being called twice. It's just crashing immediately after it deletes the first time. It's weird because when the deconstructor is called normally outside of the clear all command, it works just fine. I think it has to do with how MATLAB deletes classes and clears mex-files. But even then, I mexLocked the deconstructor mex files and was still encountering issues. I'm puzzled.
  • edited March 2012
    I looked this up a little, and there are a couple of things I've come up with. Here's how I understand it ought to work:
    Your C++ constructor returns an mxArray (i.e. the C implementation of MATLAB's sole data type) inside which you have a pointer to a C++ object. This mxArray is then being wrapped inside a handle class inside MATLAB which lets you group together all of the functionality associated with the C++ object.

    Note that this mxArray wrapping is pretty important, and it needs to be allocated with mxMalloc and mexMakeMemoryPersistent, because it needs to behave just like any other MATLAB object and be able to be garbage-collected. Also, you've probably got to be careful with how you treat that pointer value, given MATLAB's data types.

    In any case, when you do clear all there's several things being destroyed; for it to work it ought to happen in this order:
    (1) The handle object's destructor gets called, calling the MEX-destructor on your mxArray variable - both of these should still exist and retain their values and the mxArray should still hold the same pointer it held before.
    (2) The mxArray variable and the MEX-functions get destroyed (the order for these shouldn't matter).
    Post edited by lackofcheese on
  • image
    Next AI project is autonomous soaring with thermal updrafts. Here is my day 1 air current simulator.
  • That's cool.
  • edited April 2012
    This is for a class and I wouldn't be using visual basic if I didn't have to. I know for a fact this would work 1000 times better/simpler in python. X-posted from stackoverflow:
    I'm using the following code to read in a highscores file with the score written first and the player's name written to the next line, and then display the top 3 scores or fewer if there aren't 3 scores written to the file. A highscores display should have the scores sorted with the highest first so that's what this program tries to implement. Array.sort() isn't doing a damn thing. In fact, the program's not even reversing the arrays like it should either. I've tested array.sort() this way with the names and scores arrays given explicitly and it works fine. I thought maybe it was still reading the data as string even though it's declared otherwise but it rounds off decimals when I change the array type to Integer so I'm pretty sure it's not that. I have no idea why reading the data in from a file changes how it's sorted. My teacher didn't understand what was going on either. Does anyone know what's going on here?

    Imports System.IO

    Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim fileLines() = File.ReadAllLines(Application.StartupPath & "/../../Resources/highscores.txt")
    Dim highscores() As Double = {}
    Dim names() As String = {}
    'For some reason vb doesn't have a function to add items to arrays.
    'Lists have this capability, but if I used a list I couldn't use array.sort
    For i As Integer = 0 To fileLines.Length() - 1
    If (i Mod 2 = 0) Then
    Array.Resize(highscores, highscores.Length + 1)
    highscores(highscores.Length - 1) = fileLines(i)
    Array.Resize(names, names.Length + 1)
    names(names.Length - 1) = fileLines(i)
    End If
    Array.Sort(highscores, names)
    If highscores.Length() > 0 Then
    Label1.Text = Str(highscores(0)) + " " + names(0)
    End If
    If highscores.Length() > 1 Then
    Label2.Text = Str(highscores(1)) + " " + names(1)
    End If
    If highscores.Length() > 2 Then
    Label3.Text = Str(highscores(2)) + " " + names(2)
    End If
    End Sub
    End Class
    Post edited by Pegu on
  • Stackoverflow fixed me up.

  • Why aren't you reading the documentation? It will save you a lot of stress.
  • Why aren't you reading the documentation? It will save you a lot of stress.
    I was reading the array.sort documentation but it didn't help because my problem was due to an error in the file I was reading. I agree that the array.reverse documentation would have helped me, but that was a secondary issue to the array.sort issue.

  • edited April 2012
    Also list does have a sort. I know you say that you can't use Array.Sort() but why not just use list sort?
    Post edited by MATATAT on
  • edited April 2012
    I wasn't sure if that one could sort two lists together like array.sort could.
    Edit: Just checked, it can't.
    Post edited by Pegu on
  • It apparently uses the array sort so I would assume that it could.
  • Anyone have any thoughts on Prolog?
  • Anyone have any thoughts on Prolog?
    It worked pretty well when I had my automation classes. It's fun making machines do things with just a few lines of code.
  • I'm working on a program to make a robot follow a bezier curve. Basically, it generates parametric equations for x and y as polynomials in terms of t and takes the derivatives of each. It takes arctan(dy/dx) to find the angle it should currently be moving at, compares it to its current angle, and moves in an arc to adjust. It moves at a constant speed (average of left and right wheel speeds) and uses the arc length equation to find how much to increase t by based on the distance traveled.

    Is there an easier way to adjust my angle than arctan? I'm afraid that as the robot crosses vertical, the high tan values will cause problems and inaccuracy. Finding the rate of change of angle directly would be great, if possible.
  • Don't use arctan, use atan2.
  • Thanks, I didn't even knowbthat existed.
  • So I've been playing some chess on my phone and I was considering trying to make an Android chess game. While there are quite a few already I've noticed that there doesn't seem to be any with online multi. I was trying to think of easy ways to do this and I thought that one easy way might be to export to some format (PGN?) and send to the other player via SMS. I'm not really familiar with mobile development so I was wondering if someone more experienced could confirm my approach or suggest an alternative.
  • ZOMG light shaders for roguelikes
  • Ooooooooooooooh

  • I'm making a contour map based on distance to the nearest PAX across the entire Earth:

    It's a work in progress (right now it only handles distance from a single location), but it already looks fairly cool so I thought I'd post it. If you want to view the HTML file directly, use this link:

    At the moment it's just embedded Javascript using the Google Maps API. If anyone wants to contribute or make suggestions, feel free. One thing I guess it needs is a way of showing the distances corresponding to the contours; does anyone have a good idea on how to do that?
  • I don't know anything about coding, but it'd be cool if it showed distance from all PAXes instead of just East.
  • Yeah, I'm still working on that. It's not too easy.
  • Well, the multi-PAX distance is running in a basic way, though not without a couple of issues. Now I need to sleep.
  • Well, the multi-PAX distance is running in a basic way, though not without a couple of issues. Now I need to sleep.
    next astep, make it a contour based on travel time to any PAX.
    This will make Africa even worse for getting to PAX.
  • edited December 2012
    Okay, I finally fixed the contour generation; I think it works properly now. The link is still

    Also, a couple of new features:
    - Click and drag PAXes to move them around.
    - Click on an empty part of the map to make a new PAX.
    - Right click on an existing PAX to destroy it.

    We need a PAX Africa!
    next astep, make it a contour based on travel time to any PAX.
    Too hard :(
    Anyone have other suggestions?

    I figure a distance-based legend would be in order, but I haven't done one yet.
    Post edited by lackofcheese on
  • If you add a PAX JP (Tokyo) and a PAX EU (London), you can locate the secret location of PAXLantis.
  • Where's the point that's equidistant from all PAXes?
Sign In or Register to comment.