Using Java UUID in Matlab

조회 수: 24 (최근 30일)
Gurudatha Pai
Gurudatha Pai 2015년 9월 7일
편집: Andrei Veldman 2022년 3월 17일
I am trying to use JAVA UUIDs to identify the objects in my code. When I try to print it inside an fprintf, I get a weird error. I could print it in disp but that is not ideal since I want a good formatted message.
>> temp = java.util.UUID.randomUUID;
>> myuuid = temp.toString;
>> disp(myuuid)
a8a4eae2-6cc7-47d3-b58e-408b71fab260
>> fprintf(myuuid)
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Any help in understanding or fixing this error is appreciated.
  댓글 수: 1
David Galbally
David Galbally 2021년 8월 21일
>> temp = java.util.UUID.randomUUID;
>> myuuid = temp.toString;
>> MATLABstr = myuuid.toCharArray';
>> fprintf(MATLABstr)

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

답변 (3개)

Walter Roberson
Walter Roberson 2015년 9월 7일
편집: Walter Roberson 2015년 9월 7일
Try
fprintf('%s\n' myuuid);
If that doesn't work then
fprintf('%s\n', char(myuuid));
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 9월 7일
It appears that I was adding a second option to my answer just as you replied. Did you get a chance to try
fprintf('%s\n', char(myuuid));
Emile Glorieux
Emile Glorieux 2020년 8월 26일
This doesn't work for me.
>> fprintf('%s\n', char(myuuid));
Undefined function or variable 'myuuid'.

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


Philipp Tempel
Philipp Tempel 2022년 3월 17일
This may be too stupid to be true but, looking at the source code of tempname(), you can see that you can simply do
uuid = char(matlab.lang.internal.uuid())
  댓글 수: 1
Andrei Veldman
Andrei Veldman 2022년 3월 17일
편집: Andrei Veldman 2022년 3월 17일
True, but then you would be using an undocumented Matlab feature, which could change without warning...

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


Andrei Veldman
Andrei Veldman 2022년 1월 19일
...or, using pure Matlab (no need for converting form Java):
[~,guid] = fileparts(tempname)
guid = 'tp8d1a6bd2_0ac7_4c0a_8d3d_b32e4e97b746'

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by