how to call path location in global to a function in matlab?

10 visualizaciones (últimos 30 días)
hi......i created two functions(add,sub).in sample.m file, i called this two function with below paths.
path_01 = 'C:\Users\Desktop\folder\';
path_02= 'C:\Users\Desktop\folder\add\';
.the input is a .txt file is located in path_01..to access the file,i called this path_01 file globally inside add like below,but the add() function is working here..any ides to solve this??
inside function add.m
function add()
global path_01
......
....
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Ag. de 2013
Global variables are only treated as global in functions that declare them "global", or in scripts that are run from such functions. When you call upon the function add() you are declaring the variable global within add(), not within the routine that called add().
You can change add.m to be a script, or you can leave add.m as a function but inside the function use
evalin('caller', 'global path_01 path_02')
If you do not change path_01 and path_02 inside of your routines, only use the values, then another way of proceeding is to make the names into functions
function p = path_01
p = 'C:\Users\Desktop\folder\';
end
then any place you use it, such as (e.g.)
cd(path_01)
or (e.g.)
fprintf('Did not find any files in directory %s\n', path_01);
then the function will be called, the string will be returned, and everything will proceed happily, no add() needed, and no global variables needed.

Más respuestas (2)

David Sanchez
David Sanchez el 29 de Ag. de 2013
Since add is a matlab built-in function, you should not use it to name your own functions.
If path_01 and path_02 are constant paths, and they will not change in the future, avoid using them as global variables: define them inside your function instead.
function whatever_name_you_choose
path_01 = 'C:\Users\Desktop\folder\';
path_02= 'C:\Users\Desktop\folder\add\';
...
  1 comentario
sandy
sandy el 30 de Ag. de 2013
this path are not constant.paths are different depend on my file input.

Iniciar sesión para comentar.


Jan
Jan el 29 de Ag. de 2013
global variables must be defined as global in each function or script before they are used. Where did you define the value of "path_01" and did you specify it as global there also?
Btw., global variables are a bad programming style, because they provoke errors and impede the debugging. It would be cleaner to provide the folders and input arguments or store them persistently (see "doc persistent") inside a dedicated function and reply them on demand:
function S = GetMyFolder(Name)
switch Name
case '01'
S = 'C:\Users\Desktop\folder\';
case '02'
S = 'C:\Users\Desktop\folder\add\';
otherwise
error('Unknown folder name');
end
Now GetMyFolder('01') replies the wanted folder without the danger of confused globals. Perhaps a more meaningful name than '01' would be useful.
  1 comentario
sandy
sandy el 30 de Ag. de 2013
i specific this below paths in separate file(say main.m).,because this path will be changed frequently,so i globally specified this both paths inside two function.& along win main.m file, i called two functions too.
path_01 = 'C:\Users\Desktop\folder\';
path_02= 'C:\Users\Desktop\folder\add\';
i need those two function should access those files from the paths which i made global.any other alternate ways?

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations 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!

Translated by