how to use getvarname
Afficher commentaires plus anciens
PtnBaseSA2(1,:)={"TEST"}
f="PtnBaseSA2";
f1=genvarname(f)
f1(1)
10 commentaires
Dyuman Joshi
le 22 Déc 2023
Modifié(e) : Dyuman Joshi
le 22 Déc 2023
Even after asking 78 questions on MATLAB Answers, it has not occured you to provide details. Everytime someone has to ask or guess what you want to do -
Do you want to use the text "33 fdfdsfdfd" to make a variable name? If yes, then you can't that, because variable names in MATLAB can not start with numbers.
If no, then specify what is the expected outcome.
If any of your next questions are also like this, I will be closing them, until you put together a proper problem statement and provide necessary details in the question itself.
If you have a language barrier, use a Translator.
And you should really consider taking the MATLAB Onramp course. Invest 2 hours of your time and learn the basics of MATLAB yourself, it's free.
Dyuman Joshi
le 22 Déc 2023
You still haven't specified what you want to do or what is the expected output.
Stephen23
le 22 Déc 2023
"I want to use the string as if it were the name of the variable"
But why use genvarname here it all? You can get the result you want just be doing,
PtnBaseSA2={"TEST"}
f1=PtnBaseSA2
shamal
le 22 Déc 2023
"my program requires it"
I have noticed that whenever someone makes that claim they almost never back it up with an actual detailed explanation. In the rare cases that they do, it usually becomes very clear very quickly that their entire approach is inefficient and fundamentally flawed. We can then show them something simpler and more robust.
Please explain why you think that your "program requires it", because I would like to learn more about MATLAB.
Stephen23
le 23 Déc 2023
"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.
Réponse acceptée
Plus de réponses (2)
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 commentaires
f="2PtnBaseSA 2"; %Incorrect variable name
f1 = genvarname(f)
eval([f1 + '(1,:)={"TEST"}'])
eval([f1])
Image Analyst
le 22 Déc 2023
1 vote
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?
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!