How to define a text variable using an older text variable?

조회 수: 1 (최근 30일)
Martín Pérez Estébanez
Martín Pérez Estébanez 2022년 3월 24일
댓글: Stephen23 2022년 3월 24일
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
Stephen23
Stephen23 2022년 3월 24일
편집: Stephen23 2022년 3월 24일
"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:

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

채택된 답변

Voss
Voss 2022년 3월 24일
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
m = 10×1 string array
"batch1_1" "batch1_2" "batch1_3" "batch1_4" "batch1_5" "batch1_6" "batch1_7" "batch1_8" "batch1_9" "batch1_10"
n = 'batch1'; % in case you had a character vector n
m = sprintfc([n '_%d'],1:10).' % cell array of character vectors m
m = 10×1 cell array
{'batch1_1' } {'batch1_2' } {'batch1_3' } {'batch1_4' } {'batch1_5' } {'batch1_6' } {'batch1_7' } {'batch1_8' } {'batch1_9' } {'batch1_10'}
  댓글 수: 5
Voss
Voss 2022년 3월 24일
편집: Voss 2022년 3월 24일
@Stephen I was using the term "variable names" as I imagine the OP intended it - to refer to column headers in csv files (or parts of the files' names), not variables in the MATLAB workspace.
Stephen23
Stephen23 2022년 3월 24일
@_: you may be right. Why why why why did TMW pick the same terminology?

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

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 3월 24일
x=1:10;
T = compose("batch1_%d.csv",x(:))
T = 10×1 string array
"batch1_1.csv" "batch1_2.csv" "batch1_3.csv" "batch1_4.csv" "batch1_5.csv" "batch1_6.csv" "batch1_7.csv" "batch1_8.csv" "batch1_9.csv" "batch1_10.csv"

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by