how to use getvarname

9 ビュー (過去 30 日間)
Luca Re
Luca Re 2023 年 12 月 22 日
コメント済み: Stephen23 2023 年 12 月 23 日
PtnBaseSA2(1,:)={"TEST"}
PtnBaseSA2 = 1×1 cell array
{["TEST"]}
f="PtnBaseSA2";
f1=genvarname(f)
f1 = "PtnBaseSA2"
f1(1)
ans = "PtnBaseSA2"
  10 件のコメント
Luca Re
Luca Re 2023 年 12 月 22 日
編集済み: Luca Re 2023 年 12 月 22 日
ok I'll try with an example to make you understand what I have to do:
I have a text in AreaText...
The words in brackets are automatically captured
Exampe:
AreaText: ....xxx....PtnBaseSA2(32)..text...PtnB3(12)..
I catch PtnBaseSA2 and PtnB3 and i display in other AreaText the content present in the.txt file at that index
Stephen23
Stephen23 2023 年 12 月 23 日
"I have a text in AreaText..."
What is "AREATEXT? That term exist in your text file.
"...the content present in the.txt file"
Which looks very much like an M-file: why not just RUN it?
"Exampe: AreaText: ....xxx....PtnBaseSA2(32)..text...PtnB3(12).."
That is not an example: it is invalid MATLAB sytnax and I cannot run it.
"I catch PtnBaseSA2 and PtnB3 and i display in other AreaText the content present in the.txt file at that index"
If you are just trying to display file content then you definitely do NOT need to dynamically access variable names.
Please upload a sample data file. Please explain which data from that file that you want to display.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2023 年 12 月 22 日
編集済み: Matt J 2023 年 12 月 22 日
PtnBaseSA2 is an arraycell with many elements. I find the word "PtnBaseSA2" in some text and display some array indices of this arraycell.
If that's the task, then genvarname has nothing to do with it. In situations like that, you would make your words into struct fields, and use dynamic field indexing, like below. You can also do similar things with tables.
S.PtnBaseSA2={10,20,30,40,50,60}
S = struct with fields:
PtnBaseSA2: {[10] [20] [30] [40] [50] [60]}
indices=[2,5];
f="PtnBaseSA2";
S.(f)(indices)
ans = 1×2 cell array
{[20]} {[50]}
  1 件のコメント
Luca Re
Luca Re 2023 年 12 月 22 日
Ok it works thanks

サインインしてコメントする。

その他の回答 (2 件)

Ganesh
Ganesh 2023 年 12 月 22 日
編集済み: Ganesh 2023 年 12 月 22 日
Firstly, I see that there is an inconsistency between the question and the code you have written. You have asked about the usage of "getvarname" but have mentioned "genvarname" in your code.
Secondly, I assume that when you performed the operation f1(1), you expect the result to be "TEST". This is not bound to happen as you can try viewing the datatype of f1 and you will see it is a string.
Lastly, genvarname is a function that would generate a valid variable name as a string. The reason for using it would be to ensure that new valid variables can be made dynamically. This is usually implemented by using the "eval()" function. However, I advise you to be aware of the risks attached with using the "eval()" function.
Please refer to Example 2 of the following documentation to see how the function is used practically:
  2 件のコメント
Luca Re
Luca Re 2023 年 12 月 22 日
PtnBaseSA2(1,:)={"TEST"}
PtnBaseSA2 = 1×1 cell array
{["TEST"]}
f="PtnBaseSA2";
f1=getvarname(f)
Unrecognized function or variable 'getvarname'.
Ganesh
Ganesh 2023 年 12 月 22 日
f="2PtnBaseSA 2"; %Incorrect variable name
f1 = genvarname(f)
f1 = "x2PtnBaseSA2"
eval([f1 + '(1,:)={"TEST"}'])
x2PtnBaseSA2 = 1×1 cell array
{["TEST"]}
eval([f1])
x2PtnBaseSA2 = 1×1 cell array
{["TEST"]}

サインインしてコメントする。


Image Analyst
Image Analyst 2023 年 12 月 22 日
I doubt your program requires it, unless you wrote it poorly. The FAQ shows you how to do this unwise operation and tells you why you should not do it: https://matlab.fandom.com/wiki/FAQ#How_can_I_create_variables_A1,_A2,...,_A10_in_a_loop?

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by