Variable number of output arguments in a function
1 次查看(过去 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 (한국어)