store char in a column matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alberto Acri
el 18 de Jun. de 2023
Comentada: Rik
el 18 de Jun. de 2023
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?
0 comentarios
Respuesta aceptada
Rik
el 18 de Jun. de 2023
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 comentarios
Steven Lord
el 18 de Jun. de 2023
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).'
s = "parameter_" + n.^2
check = s(4) % should be "parameter_16"
Rik
el 18 de Jun. de 2023
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).
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!