How to define a text variable using an older text variable?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Martín Pérez Estébanez
am 24 Mär. 2022
Kommentiert: Stephen23
am 24 Mär. 2022
Hi, I have a bunch of csv files that I would like to import into my MATLAB. The files share a common part of the name, but contains some variations. To make it easy, I would like to make a set of names for the variables that I want to import, but I am not sure how to do it.
I will give you an example. Giving the following variable "n", how can I generate a set of "mi" string variables based in "n" like in the example?
n="batch1"
m1="batch1_1"
m2="batch1_2"
.
.
mi="batch1_i"
Thank you very much!
1 Kommentar
Stephen23
am 24 Mär. 2022
Bearbeitet: Stephen23
am 24 Mär. 2022
"To make it easy, I would like to make a set of names for the variables that I want to import,..."
It is the opposite of easy.
Naming the variables dynamically would be slow, complex, inefficient, obfsucated, liable to bugs, and difficult to debug:
"...but I am not sure how to do it."
Don't do it.
"Giving the following variable "n", how can I generate a set of "mi" string variables based in "n" like in the example"
MATLAB is designed to use arrays. You should use arrays, with indexing. Indexing is neat, simple, and very efficient (unlike what you are trying to do). Using indexing is what the MATLAB documentation shows here:
Akzeptierte Antwort
Voss
am 24 Mär. 2022
This will create a set of numbered variable names based on n. You can use the resulting m as your list of variable names.
(There are two different approaches depending on if n is a string or a character vector - it's a string in the question, like you said.)
n = "batch1"; % string n
m = string(sprintfc(n+"_%d",1:10).') % string array m
n = 'batch1'; % in case you had a character vector n
m = sprintfc([n '_%d'],1:10).' % cell array of character vectors m
5 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!