필터 지우기
필터 지우기

Inverse function to genvarname, matlab.lan​g.makeVali​dName(Str,​"Replaceme​ntStyle","​hex")

조회 수: 3 (최근 30일)
I need to use the field names of a structure to automatically generate some plot legends. Therefor I would need a function that converts a string containing hexadecimal representations of a character back to the character. Example.
VarName = genvarname("Alpha-Bravo")
VarName % Alpha0x2DBravo
Str = theSearchedFunction(VarName, ...)
Str % Alpha-Bravo
  댓글 수: 1
Stephen23
Stephen23 2022년 5월 24일
편집: Stephen23 2022년 5월 26일
There is no general solution to this, e.g. it is impossible to distinguish between these two:
genvarname("-")
ans = "x0x2D"
genvarname("x0x2D")
ans = "x0x2D"
If you can place some restrictions on the characters, then it might be possible.

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

답변 (1개)

Oisín Watkins
Oisín Watkins 2022년 5월 24일
I had this same problem, but in my case the hex string was only ever 2 charaters long, eg: '0x26' or '0x27'.
I made a simple enough function to search out those substrings, split the input string by that deliminator, then convert the deliminator and concatonate the result back in. This also works for strings with multiple hex segments. At the end all remaining characters are concatonated onto the output string.
function [output_str] = getvarname(input_str)
%getvarname undos the conversion performed by genvarname
idx = strfind(input_str, '0x');
str_to_search = input_str;
output_str = "";
for i=1:length(idx)
deliminator = input_str(idx(i):idx(i)+3);
str_parts = split(str_to_search, deliminator);
character = char(hex2dec(input_str(idx(i)+2:idx(i)+3)));
output_str = strcat(output_str, str_parts{1}, character);
str_to_search = str_parts{2};
end
output_str = strcat(output_str, input_str(idx(end)+4:end));
end
This is fairly cheap and simple, but it did the trick for me. Hope this helps!

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by