Assigning vectors to a list of names
Mostrar comentarios más antiguos
Hello everyone, I have a list of str values and to every name I want to associate a vector of two elements, but I can't do it directly.
just as an example, i want to assign the vector [1,2] to each element of the list:
vectors=['name1','name2','name3','name4','name5']
for i=1:length(vectors)
vectors(i)=[1,2]
end
but as long as vectors(i) is a str value i can't do it. Pls help me with this, I know it's very simple but I'm stuck on it.
2 comentarios
Stephen23
el 15 de Dic. de 2024
"Pls help me with this, I know it's very simple but I'm stuck on it."
John D'Errico
el 15 de Dic. de 2024
Learn to use arrays. Cell arrays. Structs. All sorts of things that CAN be used far more effectively than what you incorrectly think you want to do.
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 15 de Dic. de 2024
It isn't hard.
vectors=["name1","name2","name3","name4","name5"];
tname = tempname + ".m";
[fid, msg] = fopen(tname, 'w');
if fid < 0; error('failed to open "%s" because "%s"', tname, msg); end
for K = 1 : length(vectors)
fprintf(fid, '%s = [1 2];\n', vectors(K));
end
fclose(fid);
run(tname);
delete(tname);
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!