Error using grid. Too many input arguments.
显示 更早的评论
Hello !
I am writing because I am encountering some issues with Matlab 2018a.
I developped a custom function to quickly plot a graph. Here is my file "test.m" :
x = 1:1:100;
y = x.^3;
new_plot(x, y, 'x', 'y = x^2', struct('grid', 'on', 'legend', {'y = x^2', 'Location', 'NorthEast'}));
function new_plot(x, y, x_label, y_label, params)
if ~exist('params', 'var')
params = struct();
end
figure;
plot(x, y);
xlabel(x_label);
ylabel(y_label);
% légende
if isfield(params, 'legend') % si grilles ou non
legend(params.legend);
end
% grilles
if isfield(params, 'grid') % si grilles ou non
grid (params.grid);
end
end
But I am facing with a console error :
Error using disp
Too many input arguments.
Error in test (line 4)
new_plot(x, y, 'x', 'y = x^2', struct('grid', 'on', 'legend', {'y = x^2', 'Location', 'NorthEast'}));
I remark than this does work :
x = 1:1:100;
y = x.^3;
new_plot(x, y, 'x', 'y = x^2', struct('legend', {'y = x^2', 'Location', 'NorthEast'}));
So I can deduce ths problem is coming from my property "legend" of my struct "params".
Can you help me ?
1 个评论
Adam
2019-2-28
Using the debugger should help you locate the problem very quickly. In particular the Pause on errors (or Stop on errors in older versions) option which will land you on the line that is causing the error, from which you can use the callstack to navigate back up to your own code if it stops in the middle of the disp function.
采纳的回答
更多回答(1 个)
Fangjun Jiang
2019-2-28
0 个投票
params.grid has 3 'on' in it.
2 个评论
Robin L.
2019-2-28
Fangjun Jiang
2019-2-28
The cause is your usage of struct(). There are many ways to resolve this. The way you pass in your arguments is not typical. I am not sure what is your purpose. If you insist on a structure,
clear params
params.grid='on'
params.legend={'y = x^2', 'Location', 'NorthEast'};
plot(1:10);
grid(params.grid)
legend(params.legend{:})
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

