이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Variable number of output arguments in a function
조회 수: 3 (최근 30일)
이전 댓글 표시
giacomo
2017년 9월 28일
Hi guys. I'm sorry if this is a basic question but I'm stuck. I have a function that I need to keep very general, since it's attached to a switch menu where I test three different structural elements from three differents POV (frontal, crest, back) using another switch menu. The problem is that I need to detect colors, but colors change in type AND also in quantity. Being the max number 3, I have a problem in the crest where I just have one color. The best way would be to 'mute' the variables of the other 2 colors but how do I do it? If I put [] the program crashes. Basically sometimes I don't need to use all the variables and I need them not to interfer with the rest of the code.
답변 (1개)
KSSV
2017년 9월 28일
편집: KSSV
2017년 9월 28일
댓글 수: 10
giacomo
2017년 9월 28일
Yeah but how do you use it? I tried to put it after all of the input variables both where you define the function and in the main where you recall it but it doesn't work. I also tried to just put varargin as the only input, doesn't work either. The problem is that the program still looks for some of the variables and gives me an error: Undefined function or variable "Name-of-variable-that-I'm-trying-to-hide".
Guillaume
2017년 9월 28일
편집: Guillaume
2017년 9월 28일
it doesn't work
Your question and the details you've given are far too generic for us to know why it doesn't work, whatever it is
The problem is that the program still looks for...
The program does exactly what you wrote it to do, nothing less, nothing more. If it doesn't do what you want, then change it to do what you want. Unfortunately, as you've given so little details how anything is implemented, I don't see how we can give you more advice.
giacomo
2017년 9월 28일
Yeah I know.. take it easy man. I have a function like:
%
% function [out1, out2, out3, out4, out5, out6, out7, out8 etc etc] = SetThresholds()
switch
case 1 -> I define and use all of the variables out1 out2 etc etc -> works fine
case 2 -> I set and use only 3 variables -> crashes
case 3 -> see case2 -> crashes
end
In case2 and case3 I don't want other variables to interfer, ideally I'd like the program to not take them into account when then I recall the function into the main. For now the program is still looking for some of the variables I'm not setting since I don't want to use them, and blocks the code to flow. Did I make myself any clearer? Cheers
Adam
2017년 9월 28일
Your question is about a variable number of input arguments, but your function has no input arguments at all.
Jan
2017년 9월 28일
"A function like ..." is not useful. Obviously the details matter and your rough pseudo-code does not contain the details. This is completely unclear:
case 2 -> I set and use only 3 variables -> crashes
case 3 -> see case2 -> crashes
Please post the real code.
giacomo
2017년 9월 28일
Here it is:
% code
function [hueThresholdLowR1, hueThresholdLowR2, hueThresholdLowG, hueThresholdLowB, hueThresholdHighR1, hueThresholdHighR2, hueThresholdHighG, hueThresholdHighB, saturationThresholdLowR, saturationThresholdLowG, saturationThresholdLowB, saturationThresholdHighR, saturationThresholdHighG, saturationThresholdHighB, valueThresholdLowR, valueThresholdLowG, valueThresholdLowB, valueThresholdHighR, valueThresholdHighG, valueThresholdHighB] = SetThresholds()
try
button = menu('Which structure do you want to analise?', 'Cubipods', 'Cubes', 'Rocks');
switch button
case 1
% Cubipods
% Cubipods colors threshold
%Blue
hueThresholdLowB = 0.42;
hueThresholdHighB = 0.73;
saturationThresholdLowB = 0.68;
saturationThresholdHighB = 0.91;
valueThresholdLowB = 0.53;
valueThresholdHighB = 0.83;
%Green
hueThresholdLowG = 0.16;
hueThresholdHighG = 0.6;
saturationThresholdLowG = 0.36;
saturationThresholdHighG = 1;
valueThresholdLowG = 0;
valueThresholdHighG = 0.8;
%Red
hueThresholdLowR1 = 0.8;
hueThresholdHighR1 = 1;
hueThresholdLowR2 = 0;
hueThresholdHighR2 = 0.1;
saturationThresholdLowR = 0.6;
saturationThresholdHighR = 1;
valueThresholdLowR = 0.55;
valueThresholdHighR = 1;
case 2
% % Cubipods crest
% % Cubipods colors threshold
% %Yellow
hueThresholdLowB = 0.10;
hueThresholdHighB = 0.14;
saturationThresholdLowB = 0.4;
saturationThresholdHighB = 1;
valueThresholdLowB = 0.8;
valueThresholdHighB = 1;
case 3
%Cubipods rear side
% Cubipods colors threshold
% Purple
hueThresholdLowB = 0.76;
hueThresholdHighB = 0.94;
saturationThresholdLowB = 0.33;
saturationThresholdHighB = 0.67;
valueThresholdLowB = 0.1;
valueThresholdHighB = 0.7;
% White
hueThresholdLowG = 0.0;
hueThresholdHighG = 1;
saturationThresholdLowG = 0;
saturationThresholdHighG = 0.36;
valueThresholdLowG = 0.7;
valueThresholdHighG = 1.0;
otherwise
warning('The user didnt choose to test any kind of structure');
end
The thing I'm trying to say it's that the code works just for case1 (where I'm using all of the variables) but for case2 and case3 I need just those variables. I tried to comment the ones I didn't need or even set them to [ ]; . Cheers again
Jan
2017년 9월 28일
I'm astonished. What are "those" variables? You "tried to comment the ones I didn't need"? Where? I do not see commented variables. Therefore I still cannot guess, what the problem is.
Come on, giacomo, we want to help you. Really. So post the failing code and explain exactly, what is failing: show us the complete copy of the error message or explain the difference between your expectations and the result. Which code causes which problem? As long as you keep this information as a secret, we cannot help you to solve the problem.
giacomo
2017년 9월 28일
Let's try again. I'm adapting the SimpleColorDetectionByHue() code by Image Analyst to my needs. Link to that here: https://es.mathworks.com/matlabcentral/fileexchange/28512-simplecolordetectionbyhue-- In his code the switch menu is used to create a mask (using HSV) to detect the number of pixel of a given color. Mine is a bit different since I have to create up to 3 masks (for up to 3 colors). The problem is that if I create 3 masks it works fine, but if I create 1 or 2 it goes bad. Next I copy and paste the code so you can better understand, but since my explanations are poor you can look the Image Analyst code up to get an idea. Function definition:
% code
%%%%%%%%%%%%%
function [hueThresholdLowR1, hueThresholdLowR2, hueThresholdLowG, hueThresholdLowB, hueThresholdHighR1, hueThresholdHighR2, hueThresholdHighG, hueThresholdHighB, saturationThresholdLowR, saturationThresholdLowG, saturationThresholdLowB, saturationThresholdHighR, saturationThresholdHighG, saturationThresholdHighB, valueThresholdLowR, valueThresholdLowG, valueThresholdLowB, valueThresholdHighR, valueThresholdHighG, valueThresholdHighB] = SetThresholds()
try
button = menu('Which structure do you want to analise?', 'Cubipods', 'Cubes', 'Rocks');
switch button
case 1
% Cubipods
% Cubipods colors threshold
%Blue
hueThresholdLowB = 0.42;
hueThresholdHighB = 0.73;
saturationThresholdLowB = 0.68;
saturationThresholdHighB = 0.91;
valueThresholdLowB = 0.53;
valueThresholdHighB = 0.83;
%Green
hueThresholdLowG = 0.16;
hueThresholdHighG = 0.6;
saturationThresholdLowG = 0.36;
saturationThresholdHighG = 1;
valueThresholdLowG = 0;
valueThresholdHighG = 0.8;
%Red
hueThresholdLowR1 = 0.8;
hueThresholdHighR1 = 1;
hueThresholdLowR2 = 0;
hueThresholdHighR2 = 0.1;
saturationThresholdLowR = 0.6;
saturationThresholdHighR = 1;
valueThresholdLowR = 0.55;
valueThresholdHighR = 1;
case 2
% % Cubipods crest
% % Cubipods colors threshold
% %Yellow
hueThresholdLowB = 0.10;
hueThresholdHighB = 0.14;
saturationThresholdLowB = 0.4;
saturationThresholdHighB = 1;
valueThresholdLowB = 0.8;
valueThresholdHighB = 1;
% %Green
% hueThresholdLowG = 0.16;
% hueThresholdHighG = 0.6;
% saturationThresholdLowG = 0.36;
% saturationThresholdHighG = 1;
% valueThresholdLowG = 0;
% valueThresholdHighG = 0.8;
%
% %Red
% hueThresholdLowR1 = 0.8;
% hueThresholdHighR1 = 1;
% hueThresholdLowR2 = 0;
% hueThresholdHighR2 = 0.1;
% saturationThresholdLowR = 0.6;
% saturationThresholdHighR = 1;
% valueThresholdLowR = 0.55;
% valueThresholdHighR = 1;
case 3
%Cubipods rear side
% Cubipods colors threshold
% Purple
hueThresholdLowB = 0.76;
hueThresholdHighB = 0.94;
saturationThresholdLowB = 0.33;
saturationThresholdHighB = 0.67;
valueThresholdLowB = 0.1;
valueThresholdHighB = 0.7;
% White
hueThresholdLowG = 0.0;
hueThresholdHighG = 1;
saturationThresholdLowG = 0;
saturationThresholdHighG = 0.36;
valueThresholdLowG = 0.7;
valueThresholdHighG = 1.0;
% %Red
% hueThresholdLowR1 = 0.8;
% hueThresholdHighR1 = 1;
% hueThresholdLowR2 = 0;
% hueThresholdHighR2 = 0.1;
% saturationThresholdLowR = 0.6;
% saturationThresholdHighR = 1;
% valueThresholdLowR = 0.55;
% valueThresholdHighR = 1;
otherwise
warning('The user didnt choose to test any kind of structure');
end
The function is then recalled in the main section:
% code
%%%%%%%%%%%
[hueThresholdLowR1, hueThresholdLowG, hueThresholdLowB, hueThresholdHighR1, hueThresholdHighG, hueThresholdHighB, saturationThresholdLowR, saturationThresholdLowG, saturationThresholdLowB, saturationThresholdHighR, saturationThresholdHighG, saturationThresholdHighB, valueThresholdLowR, valueThresholdLowG, valueThresholdLowB, valueThresholdHighR, valueThresholdHighG, valueThresholdHighB] = SetThresholds()
%MASK
%
% hueMask = (hImage >= hueThresholdLow) & (hImage <= hueThresholdHigh);
% saturationMask = (sImage >= saturationThresholdLow) & (sImage <= saturationThresholdHigh);
% valueMask = (vImage >= valueThresholdLow) & (vImage <= valueThresholdHigh);
% %red
hueMaskR1 = (hImage >= hueThresholdLowR1) & (hImage <= hueThresholdHighR1);
hueMaskR2 = (hImage >= hueThresholdLowR2) & (hImage <= hueThresholdHighR2);
hueMaskR = hueMaskR1 & hueMaskR2;
saturationMaskR = (sImage >= saturationThresholdLowR) & (sImage <= saturationThresholdHighR);
valueMaskR = (vImage >= valueThresholdLowR) & (vImage <= valueThresholdHighR);
% %green
hueMaskG = (hImage >= hueThresholdLowG) & (hImage <= hueThresholdHighG);
saturationMaskG = (sImage >= saturationThresholdLowG) & (sImage <= saturationThresholdHighG);
valueMaskG = (vImage >= valueThresholdLowG) & (vImage <= valueThresholdHighG);
% %blue
hueMaskB = (hImage >= hueThresholdLowB) & (hImage <= hueThresholdHighB);
saturationMaskB = (sImage >= saturationThresholdLowB) & (sImage <= saturationThresholdHighB);
valueMaskB = (vImage >= valueThresholdLowB) & (vImage <= valueThresholdHighB);
% %combine the three masks for the three colors in a unique mask
hueMask = hueMaskR | hueMaskG | hueMaskB;
saturationMask = saturationMaskR | saturationMaskG | saturationMaskB;
valueMask = valueMaskR | valueMaskG | valueMaskB;
The error I get is:
%
Undefined function or variable "hueThresholdLowR1".
Error in menuswitch (line 292)
hueMaskR1 = (hImage >= hueThresholdLowR1) & (hImage <= hueThresholdHighR1);
My point being, I do not need a hueThresholdLowR1 variable for case2 since I'm only interested in detecting one color. Ex. case2 I only need variables ending in thresholdB since I just want yellow. Sometimes I need 3 masks, sometimes 2 sometimes 1. How can I make the code 'flexible' while keeping it as 'general' as possible? Thanks for being patient ^^
giacomo
2017년 10월 4일
For the record I solved my problem by myself avoiding a macrofunction instead creating separated functions in a switch case menu in the main.
참고 항목
카테고리
Help Center 및 File Exchange에서 Color에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)