How to pass function arguments to publish.m?
이전 댓글 표시
I have a function main.m, which publishes some function code fun.m and graphs generated by that code to html. My problem is, main() takes a structur input argument, call it 'X', and needs to pass it to fun(). I can't seem to accomplish this.
Here's a simple example:
function [] = fun(X)
plot(X.lower:X.upper,X.lower:X.upper); % plot a 45-degree line from X.lower to X.upper
end
And for the publishing function:
function [] = main(X)
pubopt.outputDir = 'c:\temp\';
pubopt.format = 'html';
pubopt.codeToEvaluate = 'fun(X)';
publish('fun.m')
end
This will result in an error - 'Attempt to reference field of non-structure array' - when it tries to evaluate fun.m
Now, in the above example, I know how to circumvent the problem - just don't use structures:
function [] = fun(lower,upper)
plot(lower:upper,lower:upper); % plot a 45-degree line from X.lower to X.upper
end
and then
function [] = main(X)
pubopt.outputDir = 'c:\temp\';
pubopt.format = 'html';
pubopt.codeToEvaluate = sprintf('fun(%f,%f)',X.lower,X.upper);
publish('fun.m')
end
However, in my real-life case, the structure X contains dozens of fields, all of which are needed by fun(). So the above quick-fix isn't an option. Rather, I want a way to pass the whole structure 'X' from main() to fun() and publish the latter.
Your help is appreciated!
Thanks!
채택된 답변
추가 답변 (2개)
Sean de Wolski
2014년 12월 1일
The string will be eval-ed so pass it in to publish telling it where to find x. For example:
x = magic(10);
publish foooo(evalin('base','x'))
foooo being:
function foooo(x)
imagesc(x)
end
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!