too many output arguments when calling axis in cellfun

4 views (last 30 days)
Hi there!
I want to use cellfun to plot figure for each element of the cell C3.
The code is:
cellfun(@(x,i){...
figure(i);...
set(gcf,'Position',[400*(i-1),50,400,300], 'color','w');...
fill(real(x),imag(x),[1,0.75,0],'EdgeColor','None');...
axis(gca,'equal','off')},...
C3,num2cell(1:length(C3)),'UniformOutput',false);
The error occurs: too many output arguments.
The problem come from
axis(gca,'equal','off')
It has no output argument.
So how to use cellfun to plot figure and modify its axis?
I know I could use for loop to plot, but the one-line code is cool.
By the way, code of this form seems difficult to fit into a one line code
axis equal off

Accepted Answer

Stephen23
Stephen23 on 10 Apr 2023
Moved: Walter Roberson on 10 Apr 2023
"It has no output argument."
Yes, it does. Every expression inside the curly-braces is evaluated and its output is requested. With the exception of comma-separated lists, every expression must provide one output.
"I know I could use for loop to plot..."
Avoid the anonymous function with CELLFUN. Either:
  • use a FOR-loop and call the code directly, or
  • use a local/nested function and CELLFUN.
"By the way, code of this form seems difficult to fit into a one line code"
Command-syntax is really just a convenience in the command window. Otherwise it is best avoided.

More Answers (1)

Walter Roberson
Walter Roberson on 10 Apr 2023
The code sequence
@(x,i){...
figure(i);...
set(gcf,'Position',[400*(i-1),50,400,300], 'color','w');...
fill(real(x),imag(x),[1,0.75,0],'EdgeColor','None');...
axis(gca,'equal','off')}
is not equivalent to executing the individual functions at the command line or as individual commands. MATLAB does not have "code blocks" like C or C++ does. {A; B} does not mean to designate a series of statements to be executed.
Instead, {} is the cell-array constructor. Each entry is to be evaluated, and the (first) return output from the command is to become the value of the corresponding cell entry. So
{...
figure(i);...
set(gcf,'Position',[400*(i-1),50,400,300], 'color','w');...
fill(real(x),imag(x),[1,0.75,0],'EdgeColor','None');...
axis(gca,'equal','off')}
is like
temp1 = figure(i);
temp2 = set(gcf,'Position',[400*(i-1),50,400,300], 'color','w');
temp3 = fill(real(x),imag(x),[1,0.75,0],'EdgeColor','None');
temp4 = axis(gca,'equal','off');
output = {temp1, temp2, temp3, temp4};
If any of the statements do not (cannot) return a value, then you would get an error.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!