MATLAB Answers

Robin L.
0

Error using grid. Too many input arguments.

Robin L.
さんによって質問されました 2019 年 2 月 28 日
最新アクティビティ Fangjun Jiang
さんによって コメントされました 2019 年 2 月 28 日
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.

サインイン to comment.

2 件の回答

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 件のコメント

Robin L.
2019 年 2 月 28 日
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 😃
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.
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 !

サインイン to comment.


Fangjun Jiang
回答者: Fangjun Jiang
2019 年 2 月 28 日

params.grid has 3 'on' in it.

  2 件のコメント

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
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{:})

サインイン to comment.



Translated by