Hi. I have a for loop that generates char like this: 'parameter_1' ('parameter_2' and so on).
I would like to store them all in a matrix M. Then have on the workspace something like this:
M = [parameter_1;
parameter_2;
...]
Is there an easy way to do this?

 채택된 답변

Rik
Rik 2023년 6월 18일

0 개 추천

If you use a cell array instead, the parameters don't need to have the same number of characters. That will also make it easy to convert the a string and back, should you ever need to.

댓글 수: 3

Alberto Acri
Alberto Acri 2023년 6월 18일
Thanks, I will try using cells!
If you don't need them as char arrays, I recommend using a string array instead of a cell array containing char arrays. You can even vectorize the creation of the text data using the + operator for strings.
n = (1:5).'
n = 5×1
1 2 3 4 5
s = "parameter_" + n.^2
s = 5×1 string array
"parameter_1" "parameter_4" "parameter_9" "parameter_16" "parameter_25"
check = s(4) % should be "parameter_16"
check = "parameter_16"
Rik
Rik 2023년 6월 18일
Just to add the final point: if you are using cells you need to use {} to access the content, otherwise you're dealing with the container (i.e. the cell element).

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2023년 6월 18일

댓글:

Rik
2023년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by