필터 지우기
필터 지우기

Write a function to show a square “random” image. The function will take in “at least” one input argument which is the size of the image in pixels. As a default, the image will be shown in black-white. If a second input argument is provided, it will

조회 수: 1 (최근 30일)
This is what I have so far:
function week7hw1(pixels, varargin)
%This function will receive the size of the pixels as an input
%if a second input is entered, the image will be the colormap
n=nargin;
mat=randi(pixels);
if n==0
image(mat)
elseif n==1;
colormap(varargin)
image(mat)
end
end
I'm not sure what I am doing wrong for this function.

채택된 답변

John D'Errico
John D'Errico 2019년 7월 21일
편집: John D'Errico 2019년 7월 21일
Your problem is that you misunderstand nargin.
By way of example, I wrote this very simple function.
function week7hw1(pixels, varargin)
nargin
numel(varargin)
So all it does is write out the number of elements in varargin, as well as write out the value of nargin. Now I'll try it out.
week7hw1(1,2)
ans =
2
ans =
1
Hmm. there were two inputs to the function.
Nargin was 2. But numel(varargin) was 1, as expected.
So, what does nargin do? It is NOT the number of elements in varargin. nargin is the TOTAL number of input arguments to the function. Similarly, if I provide no second argument, then what happens?
week7hw1(1)
ans =
1
ans =
0
Again, I provided no second argument. nargin was 1, since there was ONE argument. varargin was an empty array this time.
So, why does your code fail? You test to see if nargin is ZERO or ONE. But nargin will always be at least 1, because you always are providing the first argument. So you want to distinguish if there are 1 or 2 arguments provided. The necessary fix to your code is very simple.
  댓글 수: 2
Caitlin Schmidt
Caitlin Schmidt 2019년 7월 21일
This has helped and I change what n is equal to. Now, I try to run the function with two inputs and I am receiving an error with colormap(varargin). Do you know what I may do to fix this?
Thank you!
John D'Errico
John D'Errico 2019년 7월 21일
편집: John D'Errico 2019년 7월 21일
That part is easy. varargin is a CELL array. This happens because your additional arguments may be all sorts of things. So it must contain anything. But colormap cannot take a cell array as input. Instead, you need to extract that element from the cell array.
colormap(varargin{1})
You index a cell array using {} curly braces, in order to extract an element of that array. So the line as I wrote it extracts the first element of varargin, then passes that into colormap.

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

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 21일
편집: KALYAN ACHARJYA 2019년 7월 21일
function image1=rand_image()
pixel_num=input('Enter the value ');
image1=logical(randi(2,[pixel_num pixel_num])-1); %@Image Analyst Answer
imshow(image1);
end
Command Window:
>> y=rand_image();
Enter the value 100
Figure Window:
test_111.png
Second Part is not complete-
If a second input argument is provided, it will
  댓글 수: 3
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 21일
편집: KALYAN ACHARJYA 2019년 7월 21일
function image1=rand_image(pixel_num)
image1=logical(randi(2,[pixel_num pixel_num])-1); %@Image Analyst Answer
imshow(image1);
end
Now pass the pixel value from here
y=rand_image(100)
Caitlin Schmidt
Caitlin Schmidt 2019년 7월 21일
편집: Caitlin Schmidt 2019년 7월 21일
This portion has worked, however, I need the option to have a second input of a colormap. I used the if elseif statement shown above, but a figure window does not even appear when I try to run it with a second colormap input. For example, if I entered week7hw1(200,hsv) with the input of pixels and a colormap, nothing happens. Any ideas with what I am doing wrong?

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

카테고리

Help CenterFile Exchange에서 White에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by