Borrar filtros
Borrar filtros

How to use "run command" within a function which saves all variables inside base workspace?

22 visualizaciones (últimos 30 días)
function example()
%%%%
---
run('sample.m');
---
end
Is there any way, which can help to save all variable of "sample.m" script in base workspace rather than current function workspace?

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Abr. de 2018
No. run is defined as executing a script, and scripts always have access to the current workspace and affect the current workspace (though in some very recent MATLAB versions there are starting to be some ways in which scripts do not have full access to private information.)
You could create a wrapper function that looked in its parent function and copied all of the parent's variables into the current workspace, and then ran the script, and then looked at all remaining variables and compared them to the parent's copies of the variables in order to determine which had changed, and then write the changed versions to the base workspace. This would be somewhat troublesome, but I guess there might be some use for it.
If you did not want the script to have access to any of the current workspace's variables while it ran, then you could
evalin('base', 'run(''script.m'')')
  2 comentarios
Shritesh Jibhkate
Shritesh Jibhkate el 9 de Abr. de 2018
If I am using following instruction within function
run([PathName '/' FileName]);
its working fine
But,use of instruction
evalin('base','run(''[ProposalPathName '/' ProposalFileName]'')');
producing following error
" Error using / Matrix dimensions must agree.___"
Can you help me on this?
Walter Roberson
Walter Roberson el 9 de Abr. de 2018
script_path = fullfile(PathName, FileName);
cmd = sprintf('run(''%s'')', script_path);
evalin('base', cmd)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by