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

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?

 Accepted Answer

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 Comments

My aim is to plot arrays with imagesc by using my function.
I want my function to be able to plot an array without introducing sometime x and y.
I know by writing only image (a) I will get an error since my program want 3 arguments and not only one.
how can I fix this?
The example I gave does not require three arguments. It can be used with one argument or with three.
By using varargin as in the above example, the arguments you pass to your function img() get passed straight to imagesc(). Therefore, you can call img() using any valid syntax for imagesc().
yes but sometime in my code i should specifies the axis and sometime i don't to plot the array.
i want my code to be able to run as the following example:
example:
a=rand(2,3)
img(a)
or
x=[2,4,6]
y=[5,10]
img(x,y,a)
Yes. As I show in the example, it will run either way. Using varargin this way makes your function into a transparent wrapper for imagesc(). You can call it any way that you would call imagesc() alone.
a = rand(2,3);
img(a)
x = [2,4,6];
y = [5,10];
img(x,y,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
this is my code:
function img(x,y,a)
if(length(x)==1 && length(y)==1)
% i wrote this bcz i was using img(1,1,a) to plot "a" in a way to avoid the error
[xx yy]=size(a);
x=linspace(1,yy,yy);
y=linspace(1,xx,xx);
end
%%
if (length(x)~=1 && length(y)~=1)
[x1,y1]=size(a);
if(x1==length(y) && y1==length(x))
[xx yy]=size(a);
s=[xx yy];
dx = (x(end)-x(1))/(yy-1);
dy = (y(end)-y(1))/(xx-1);
xg = linspace(x(1)-dx/2,x(end)+dx/2,yy+1);
yg = linspace(y(1)-dy/2,y(end)+dy/2,xx+1);
hi = imagesc(x,y,a);
set(gca,'YDir','normal');
impixelinfo;
hold on;
hm = mesh(xg,yg,zeros(s+1));
hm.FaceColor = 'none';
hm.EdgeColor = 'k';
hm.EdgeAlpha=0.08;
else
error('Matrix dimensions must agree.')
end
else
error('Color data input must be a matrix.')
end
end
When I try to use varargin I will have an error in the second part in the code can you help me solving this problem?
Omitting the workaround conditionals, you'd get this:
function img(varargin)
% IMG(A)
% IMG(X,Y,A)
% Describe what this does
%
% A is an image array
% X, Y are vectors defining the pixel coordinates of A
% Describe the constraints on X and Y
%
% See also: image, imagesc, imshow
% handle the inputs
if numel(varargin) == 1
a = varargin{1};
[xx yy] = size(a);
x = linspace(1,yy,yy);
y = linspace(1,xx,xx);
elseif numel(varargin) == 3
[x y a] = deal(varargin{1:3});
else
error('IMG: incorrect number of arguments')
end
[xx yy] = size(a);
if (xx==numel(y) && yy==numel(x)) % this condition is not necessary
dx = (x(end)-x(1))/(yy-1);
dy = (y(end)-y(1))/(xx-1);
xg = linspace(x(1)-dx/2,x(end)+dx/2,yy+1);
yg = linspace(y(1)-dy/2,y(end)+dy/2,xx+1);
imagesc(x,y,a);
set(gca,'YDir','normal');
impixelinfo;
hold on;
hm = mesh(xg,yg,zeros([xx yy]+1));
hm.FaceColor = 'none';
hm.EdgeColor = 'k';
hm.EdgeAlpha = 0.08;
else
error('IMG: Length of X and Y must match the size of A')
end
end
As the comment notes, it's not necessary that x and y vectors match the geometry of a. When given x and y vectors, imagesc() only uses the first and last elements and assumes linearity between the two. For example, this
imagesc([0 10 1],[0 20 2],a)
will behave identically to this.
imagesc([0 1],[0 2],a
With that in mind, the constraint that x and y must match the size of a isn't needed unless it's a convention that you want to enforce yourself.
Note that the call to imagesc() no longer directly uses varargin. This means that in this example, img() is not transparent, and additional syntax like
% imagesc(fighandle,...)
or
% imagesc(...,colorlimits)
are no longer accessible. This may not be of any concern, depending on what you want to do with your function.
Wow, that is great.
I still have one more question if that's possible:
how can I remove the interaction of the Bruch with the grid line ?
i wrote a small example that shows the interaction of the bruch with the grid line in red x
command windows:
a=rand(2,3)
a =
0.5949 0.6028 0.2217
0.2622 0.7112 0.1174
>> img(a)
hold on
plot(a(1,:),'b')
result:
As I mentioned in the other question, I don't think that the data brush has any means of specifying which objects it selects. My attempts to disable the interaction by setting properties of the mesh object did not change the behavior. The only other alternative I could think of was to use the axes grid instead of mesh(), which carries its own problems.
thank you a lot DGM !!, I really appreciate your help.
the code run perfeclty

Sign in to comment.

More Answers (1)

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 Comments

My aim is to plot arrays with imagesc by using my function.
I want my function to be able to plot an array without introducing sometime x and y.
I know by writing only image (a) I will get an error since my program want 3 arguments and not only one.
how can I fix this?
The Solution provided by DGM should do exactly that. Did you get an error when trying it?
varargin stands for Variable-length input argument list in matlab. It allows you to pass any number of arguments to your function and they will be available in the function as a cell array. You can access them with
varargin{1}, varargin{2}, ...
Or simply
varargin{:}
If you want all of the arguments at once.
In the solution provided by DGM you should be able to call your function with either a or a,x,y and the arguments will be passed to imagesc in the same order.

Sign in to comment.

Asked:

on 14 Jan 2022

Commented:

on 18 Jan 2022

Community Treasure Hunt

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

Start Hunting!