Hello!
I am trying to create variable names automatically, such as:
Name_1
Name_2
Name_3
...
Name_N
To assign a different value to each one.
Do you know how to do it, any idea?

댓글 수: 8

Walter Roberson
Walter Roberson 2023년 2월 10일
Why do you want to do this?
Stephen23
Stephen23 2023년 2월 10일
편집: Stephen23 2023년 2월 10일
"Do you know how to do it, any idea?"
Yes, if you want to force yourself into writing slow, complex, inefficient, obfuscated code that is buggy and hard to debug:
The standard MATLAB approach is to use indexing. Indexing is explained in the introductory tutorials:
Indexing is neat, simple, and very efficient, unlike your approach. Indexing is a MATLAB superpower. If you do not learn how to use indexing, then using MATLAB will be very... difficult.
Ohh, okay thanks! ;) @Stephen23
I see it's a bad idea hahaha. I need to improve at code optimisation.
It's for connectivity matrices in Finite Element Method. @Walter Roberson
Jan
Jan 2023년 2월 10일
@FERNANDO CALVO RODRIGUEZ: Follow Stephen's valuable suggestion. Creating variables dynamically is a shot in your knee.
John D'Errico
John D'Errico 2023년 2월 10일
There are limits of course. If you want to create two variable names, calling them x1 and x2, go for it. Maybe that makes sense. You have two points, and want to refer to them in code. Personally it is something I do. I'll admit that it is easier and faster to type x1 than to type x(1). But my limit is usually at most 2 such variables, and only when it makes some sense to have them as different variables instead of a vector.
Once you start to create whole sets of numbered names, you are going in the wrong direction. That is when it is time to start using vectors and arrays. Your code will improve by leaps and bounds when you start to do so too.
chrisw23
chrisw23 2023년 2월 13일
may be field names are a solution
define an empty struct
myStruct = []
and add fieldnames dynamically
myStruct.(<varName>) = myVal
Stephen23
Stephen23 2023년 2월 13일
@chrisw23: given the sequentially-numbered names shown in the original question, basic indexing would be simpler and much more efficient.

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

답변 (1개)

埃博拉酱
埃博拉酱 2023년 2월 13일

0 개 추천

NameIndex=1;
Value=2;
eval(sprintf('Name_%u=%d',NameIndex,Value));
Name_1 = 2
This is possible by eval but as mentioned in the comments section, it is not recommended if there is not a very good reason.

댓글 수: 1

Stephen23
Stephen23 2023년 2월 13일
편집: Stephen23 2023년 2월 13일
The name "MATLAB" comes from "MATrix LABoratory", i.e. it is designed to work efficiently with arrays and indexing. Indexing is a MATLAB superpower. If you do not use indexing, then using MATLAB will be... difficult. The name "MATLAB" does not come from "lets store the data in lots and lots of separate variables and make our data harder to work with":

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 2월 10일

편집:

2023년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by