Generate UUID in MATLAB

1. Generate Random UUID
2. Generate Timestamp-based UUID
3. Generate Customized UUID

1. Generate Random UUID

To generate a random UUID in MATLAB, you can make use of the built-in uuid function from the java.util package. This method generates a version 4 UUID, which is based on random numbers.

        % Generate a random UUID
        javaUUID = char(java.util.UUID.randomUUID());
        disp(['Random UUID: ', javaUUID]);
    

2. Generate Timestamp-based UUID

If you want a UUID based on current timestamp, you can use MATLAB's datetime and uuid functions together. This will generate a timestamp-based version 1 UUID.

        % Generate a timestamp-based UUID
        timestampStr = datestr(datetime('now', 'TimeZone', 'UTC'), 'yyyy-mm-dd HH:MM:ss.FFF');
        timestampUUID = char(java.util.UUID.nameUUIDFromBytes(uint8(timestampStr)));
        disp(['Timestamp-based UUID: ', timestampUUID]);
    

3. Generate Customized UUID

If you need to generate a UUID with specific properties, such as providing a namespace and a custom name, you can use the uuid5 function from the uuid package.

        % Generate a customized UUID
        javaUUID = char(java.util.UUID.nameUUIDFromBytes(uint8('my-namespace', 'my-name')));
        disp(['Customized UUID: ', javaUUID]);
    

Summary

In this tutorial, we explored three different ways of generating UUIDs in MATLAB. We learned how to generate a random UUID using the uuid function from the java.util package. We also saw how to generate timestamp-based UUIDs using MATLAB's datetime and uuid functions. Lastly, we discovered how to generate customized UUIDs using the uuid5 function.

References