Assign values to a new variable via a function
Mostrar comentarios más antiguos
I have a repetitve task that I want to turn into a function. The process that I am repeating at the moment for many variables is:
load('names_v1.mat')
names_v2 = names_v1;
save('names_v2','names_v2')
To turn this process into a function the bit I am struggling with is line two. That is in the below function the line namesNew = namesOld will obviously not work becuase namesOld is the string with the variable name and not the variable itself. I was trying to use the variableValues function but that doesn't seem appropriate
function test = fileRet(oldVersion, newVersion)
% old verion might be 'v1'
% new version might be 'v2'
namesOld = strcat('names_', oldVersion);
namesNew = strcat('names_', newVersion);
load(namesOld)
% Below line is the line that doesn't work
namesNew = namesOld;
save(namesNew,namesNew)
end
Any ideas
1 comentario
Stephen23
el 13 de Dic. de 2018
"...will obviously not work becuase namesOld is the string with the variable name and not the variable itself. I was trying to use the variableValues function but that doesn't seem appropriate"
Indeed, it is not appropriate at all.
"Any ideas"
Do NOT try to magically access variable names, unless you want to force yourself into writing slow, complex, buggy code that is hard to debug. Read this to know why:
Respuesta aceptada
Más respuestas (1)
madhan ravi
el 13 de Dic. de 2018
0 votos
sprintf()
1 comentario
Tatiana Bernard
el 13 de Dic. de 2018
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!