How to use variable value as an input to the function?

조회 수: 2 (최근 30일)
Marcin
Marcin 2013년 7월 31일
I have the function which takes two arguments:
match(template1 , template2);
I want to save the first and second argument name as a string:
name1 = 'template1'; name2 = 'template2';
And then pass this names into my match function:
match( name1 , name2 )
MATLAB reads it as strings but I want it to be pointers to to template1 and template2. Do you know how to achieve this?
  댓글 수: 2
dpb
dpb 2013년 7월 31일
Better back up and describe what end objective is--Matlab is not C so talking about pointers is generally an attempt to implement something as if it were.
Marcin
Marcin 2013년 8월 1일
I'm sorry, the thing is that I have 100 templates and I need to compare them to each other by the function match, doing this manually would be 10000 lines of code, is it possible to write for loop to produce the results:
result(n)(m) = match( template(n) , template(m) )
where m and n are variables.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2013년 7월 31일
function r = match(t1 , t2)
name1 = inputname(1);
name2 = inputname(2);
r = match_by_name(name1, name2);
end
where match_by_name is your second "match" function.
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 8월 1일
nT = length(template);
result = cell(nT,nT);
for K = 1 : nT
result{K,K} = match(template{K}, template{K}); %match against self??
for L = K + 1, nT
result{K, L} = match(template{K}, template{L});
end
end

댓글을 달려면 로그인하십시오.


Richard Brown
Richard Brown 2013년 7월 31일
You can very probably get away with just passing template1 and template2 themselves (not trying to create references to them).
Matlab won't create copies unless you modify them within your match function.

카테고리

Help CenterFile Exchange에서 Adding custom doc에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by