too many output arguments when calling axis in cellfun

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

 채택된 답변

Stephen23
Stephen23 2023년 4월 10일
이동: Walter Roberson 2023년 4월 10일

0 개 추천

"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.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 4월 10일
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.

카테고리

제품

릴리스

R2021b

질문:

2023년 4월 9일

답변:

2023년 4월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by