I want to preserve the value of variable q
in the below mentioned code when it makes a call to the funtion gm
.
我想在调用函数gm时保留下面提到的代码中的变量q的值。
demo.m
is:
for q=1:Nqueries
disp(['Matching query ' db.queries{q}]);
qPath=[db.folder '/' db.fqueryFolder '/' db.queries{q} '_' featMethod '_' num2str(PeakThreshold) '.mat'];
fq=load(qPath);
query_path=[db.folder '/' db.queryFolder '/' db.queries{q} '.jpg'];
matches=cell(1,Nrefs);
fr=cell(1,Nrefs);
ref_paths=cell(1,Nrefs);
for r=1:Nrefs
rPath=[db.folder '/' db.frefFolder '/' db.references{r} '_' featMethod '_' num2str(PeakThreshold) '.mat'];
ref_paths{r}=[db.folder '/' db.refFolder '/' db.references{r} '.jpg'];
fr{r}=load(rPath);
%Matching things
[idx, dists] = vl_ubcmatch(fq.d,fr{r}.d,thRatio);
matches{r}.idx=idx;
matches{r}.dists=dists;
end
%We run the Generative Model
sim(q,:)=gm(query_path,ref_paths,fq,fr,matches,K);
end
and this code generates following error:
并且此代码生成以下错误:
Matching query 1
??? Undefined function or variable 'q'.
Error in ==> gm at 86
Iq=imread(sprintf('db/queries/%d.jpg',q));
Error in ==> demo at 65
sim(q,:)=gm(query_path,ref_paths,fq,fr,matches,K);
The gm
function uses q
as follows:
gm函数使用q如下:
Iq=imread(sprintf('db/queries/%d.jpg',q));
2 个解决方案
#1
Adding more variables to the function call is the cleanest way of resolving this issue, of course. But if modifying the called function is too painful, e.g. because you'd have to change many functions until you reach the one where you want to use your variable, you might want to consider making this variable a global variable:
当然,向函数调用添加更多变量是解决此问题的最简洁方法。但是,如果修改被调用函数太痛苦,例如因为在到达要使用变量的函数之前,必须更改许多函数,您可能需要考虑将此变量设置为全局变量:
global YOURVARIABLE %choose a good name here to avoid
%overwriting existing global variables
YOURVARIABLE
can now be accessed from any other function's workspace although you have to declare this in each function separately, see: Declaring a global variable in MATLAB
现在可以从任何其他函数的工作空间访问YOURVARIABLE,尽管您必须分别在每个函数中声明它,请参阅:在MATLAB中声明全局变量
Also, you should be very careful when using them:
此外,使用它们时应该非常小心:
http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
As described in the documentation global variables are risky because they have their own workspace that can be edited from anywhere, so if the same variable is used by several functions you might get unexpected results. Therefore, they should only be used when really necessary.
如文档中所述,全局变量存在风险,因为它们有自己的工作空间,可以从任何地方进行编辑,因此如果多个函数使用相同的变量,您可能会得到意外的结果。因此,它们只应在真正必要时使用。
#2
I modified the code in the for
loop to
我修改了for循环中的代码
sim(q,:)=gm(query_path,ref_paths,fq,fr,matches,K,q);
and the definition of the called function gm
as
和被调用函数gm的定义为
gm(query_path,ref_paths,fq,fr,matches,K,q);
#1
Adding more variables to the function call is the cleanest way of resolving this issue, of course. But if modifying the called function is too painful, e.g. because you'd have to change many functions until you reach the one where you want to use your variable, you might want to consider making this variable a global variable:
当然,向函数调用添加更多变量是解决此问题的最简洁方法。但是,如果修改被调用函数太痛苦,例如因为在到达要使用变量的函数之前,必须更改许多函数,您可能需要考虑将此变量设置为全局变量:
global YOURVARIABLE %choose a good name here to avoid
%overwriting existing global variables
YOURVARIABLE
can now be accessed from any other function's workspace although you have to declare this in each function separately, see: Declaring a global variable in MATLAB
现在可以从任何其他函数的工作空间访问YOURVARIABLE,尽管您必须分别在每个函数中声明它,请参阅:在MATLAB中声明全局变量
Also, you should be very careful when using them:
此外,使用它们时应该非常小心:
http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
As described in the documentation global variables are risky because they have their own workspace that can be edited from anywhere, so if the same variable is used by several functions you might get unexpected results. Therefore, they should only be used when really necessary.
如文档中所述,全局变量存在风险,因为它们有自己的工作空间,可以从任何地方进行编辑,因此如果多个函数使用相同的变量,您可能会得到意外的结果。因此,它们只应在真正必要时使用。
#2
I modified the code in the for
loop to
我修改了for循环中的代码
sim(q,:)=gm(query_path,ref_paths,fq,fr,matches,K,q);
and the definition of the called function gm
as
和被调用函数gm的定义为
gm(query_path,ref_paths,fq,fr,matches,K,q);