How to pass function arguments to publish.m?

1 vue (au cours des 30 derniers jours)
Yoel Lax
Yoel Lax le 1 Déc 2014
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!

Réponse acceptée

Yoel Lax
Yoel Lax le 4 Déc 2014
For the record, I solved this by using assignin:
function []=main()
x = magic(10);
assignin('base','x',x);
pubopt.codeToEvaluate = sprintf('imagesc(%s)','x');
pubopt.outputDir='c:\temp\';
publish('imagesc.m',pubopt)
end

Plus de réponses (2)

Sean de Wolski
Sean de Wolski le 1 Déc 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
Yoel Lax le 1 Déc 2014
Thanks, but this doesn't quite work yet. I think the problem is that in my real-life case, the input to the function to be published is defined within the function which tries to publish it. But once publish.m runs, it doesn't seem to know what the caller function is.
Using your example, my code looks like this (note i've substituted 'caller' for 'base'):
function []=main()
x = magic(10);
pubopt.codeToEvaluate = sprintf('imagesc(evalin(''caller'',''x''))');
pubopt.outputDir='c:\temp\';
publish('imagesc.m',pubopt)
end
Running this gives the following error:
Error using evalmxdom>instrumentAndRun (line 83)
Attempt to add "x" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in evalmxdom (line 21)
[data,text,laste] = instrumentAndRun(file,cellBoundaries,imageDir,imagePrefix,options);
Error in publish (line 168)
dom = evalmxdom(file,dom,cellBoundaries,prefix,imageDir,outputDir,options);
Error in crap (line 6)
publish('imagesc.m',pubopt)
Any ideas?
Thanks again.

Catégories

En savoir plus sur Scripts dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by