MATLAB Answers

0

Error using grid. Too many input arguments.

Asked by Robin L. on 28 Feb 2019
Latest activity Commented on by Fangjun Jiang on 28 Feb 2019
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 Comment

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.

Sign in to comment.

2 Answers

Answer by Sven
on 28 Feb 2019
 Accepted Answer

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 Comments

Thank you a lot Sven !
I realized through you response that location ain't to be set inside the legend, as I was thinking. So first I have to move my " } ", as follow :
new_plot(x, y, 'x', 'y = x^2', struct('grid', 'on', 'legend', {'y = x^2'}, 'Location', 'SouthEast'));
Then, I can use my own function, updated with your code :
myLegend = legend(params.legend);
if isfield(params, 'Location') % si location légende
myLegend.Location = params.Location;
end
And it works !
Merci beaucoup 😃
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'}})
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 !

Sign in to comment.


Answer by Fangjun Jiang on 28 Feb 2019

params.grid has 3 'on' in it.

  2 Comments

Thank you Fangjun Jiang !
Indeed.. :
test.jpg
But why ? Does that mean I have to "encapsulate" the legend ? And how ?
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{:})

Sign in to comment.