Error using grid. Too many input arguments.

3 次查看(过去 30 天)
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
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.

请先登录,再进行评论。

采纳的回答

Sven
Sven 2019-2-28
As already pointed out, just look at what gets constructed if you call
struct('grid', 'on', 'legend', {'y = x^2', 'Location', 'NorthEast'}))
This should clear up why your code failed. And by the way, setting the location also fails/gets ignored when deleting the grid part, you just didn't notice, because you specified the default location of a legend.
These changes will make your code work, but there might be a better way of course.
x = 1:1:100;
y = x.^3;
myPlotStruct.grid = 'on';
myPlotStruct.legend = 'y = x^2';
myPlotStruct.Location = 'SouthEast';
new_plot(x, y, 'x', 'y = x^2', myPlotStruct);
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
myLegend = legend(params.legend);
if isfield(params, 'Location') % si grilles ou non
myLegend.Location = params.Location;
end
end
% grilles
if isfield(params, 'grid') % si grilles ou non
grid(params.grid);
end
end
  3 个评论
Steven Lord
Steven Lord 2019-2-28
To elaborate on what Sven said, the reason why the struct isn't what you expected is because your call falls into the second bullet in the "s = struct(field, value)" syntax listed on the struct documentation page.
To make a struct where one of the fields is a cell either assign that field to the struct after it is constructed, using "structname.fieldname = cellvalue" or wrap your cell inside another cell as per the "Fields with Cell Arrays" example on that documentation page.
Assign to the field after it is constructed:
S = struct('foo', 1);
S.bar = {'abc', 'def'}
Wrap the cell value inside another cell:
S2 = struct('foo', 1, 'bar', {{'abc', 'def'}})
Robin L.
Robin L. 2019-2-28
Thank you Steven Lord !
I tried :
a = struct('foo', 1, 'bar', {{'abc', 'def'}});
b = struct('foo', 1, 'bar', {'abc', 'def'});
I get :
test2.jpg
I undestood !
Not that easy as I was thinking, thanks !

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2019-2-28
params.grid has 3 'on' in it.
  2 个评论
Robin L.
Robin L. 2019-2-28
Thank you Fangjun Jiang !
Indeed.. :
test.jpg
But why ? Does that mean I have to "encapsulate" the legend ? And how ?
Fangjun Jiang
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{:})

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by