How to pass function arguments to publish.m?
Show older comments
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!
Accepted Answer
More Answers (2)
Sean de Wolski
on 1 Dec 2014
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
Yoel Lax
on 1 Dec 2014
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!