how can i use my function directly on a array without having x and y?

조회 수: 2(최근 30일)
Rabih Sokhen
Rabih Sokhen 2022년 1월 14일
댓글: Rabih Sokhen 2022년 1월 18일
the code is:
function img(x,y,a)
imagesc(x,y,a);
set(gca,'YDir','normal');
impixelinfo;
end
command windows:
a=rand(2,3)
a =
0.5078 0.3828 0.7304
0.3943 0.8834 0.7662
>> img(a)
Not enough input arguments.
hello guys
i am trying to creat a small function similar to imagesc by adding more option to the original imagesc function,
however i can't use my img function to a array directly
exemple, if i try to do img(a) and a= rand(2,3) i will get a error "Not enough input arguments."
any idea how can i use my function directly on a array without having x and y?

채택된 답변

DGM
DGM 2022년 1월 14일
You don't need to specify x and y when calling imagesc().
If you handle the input arguments like this:
A = imread('cameraman.tif');
img([0 1],[0 1],A)
img(A)
function img(varargin)
imagesc(varargin{:});
set(gca,'YDir','normal');
impixelinfo; % this should work fine, but won't show up in the web-figure
end
then you can call it either way.
  댓글 수: 9
Rabih Sokhen
Rabih Sokhen 2022년 1월 18일
thank you a lot DGM !!, I really appreciate your help.
the code run perfeclty

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Max Heimann
Max Heimann 2022년 1월 14일
The function you defined has 3 arguments. x,y and a.
function img(x,y,a)
imagesc(x,y,a);
set(gca,'YDir','normal');
impixelinfo;
end
Yet you call it with just one argument "a".
a=rand(2,3)
a =
0.5078 0.3828 0.7304
0.3943 0.8834 0.7662
>> img(a)
The error message is pretty clear here. Where are the arguments x and y supposed to come from if you dont pass them as function arguments. Either somehow calculate the appropriate values in your function, hardcode them there or pass them as arguments.
  댓글 수: 3
Rabih Sokhen
Rabih Sokhen 2022년 1월 18일
thank you Max for your response

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by