Save each pair in container.Map to separate variable in .mat file
Mostrar comentarios más antiguos
Hi I Have a container.Map object in which I need each key, value pair to be saved to a variable within a mat file.
Was originaly doing this with eval but then discovered assignin. Is there a better way to do this?
I need the outputted mat file to contain individual variables and so can't just use list due to the application this .mat file will be used in.
Simplified code snippet below
M = container.Map();
M("var1") = [1 2 3];
M("var2") = [4 5 6];
saveVars = {};
for key = M.keys()
assignin('base', key, M(key))
saveVars(end+1) = key
end
save("output.mat", saveVars{:})
2 comentarios
"Was originaly doing this with eval but then discovered assignin."
Replacing eval with assignin does not avoid any of the problems of dynamic variable names, in fact you just add extra obfuscation and latent bugs to the process (i.e. as well as having all of the disadvantages of using eval).
Jack Kershaw
el 1 de Feb. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements 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!