Error in Matlab, calling a function - matlab

The errors are:
Error in getvalues (line 4)
faceNoNoise = wiener2(x, [5 5]);
Output argument "mouthTall" (and maybe others) not assigned during call to "C:\Users\Trent\face\getvalues.m>getvalues".
Error in finalProject2 (line 10)
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray )
faceNoNoise = wiener2(x, [5 5]); <---- getvalues.m line with error
function finalProject2(x) <----- finalproject file
face = imread(x);
faceGray = rgb2gray(face);
numWhiteEyebrow = 0;
mouthTall = 0;
eyebrowHeight = 0;
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray ) <--error above
end
Im trying to figure out why its doing this...

This happens since you have not assigned required output values in the function getvalues.m. There are 3 output arguments which should be returned by getvalues.m. See this and this. To clarify even more, if your function definition in getvalues.m file is [a,b,c]=getvalues(x) (and this is how it would mostly be in your case) then in your getvalue.m file, you should have variables a, b and c.

Related

Matlab Load from relative path

function []= read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = char(dir_list(i));
feat_files = dir([dir_str, '/*.res5b']);
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = strcat(dir_str, '/', feat_files(j).name);
...............
....................so on
Give me error like
Error using dir
Invalid path. The path must not contain a null character.
Error in read_c3d_feat (line 12)
feat_files = dir([dir_str, '/*.res5b']);
Your dir_list variable must have strings which contain null characters, as the error tells you. If you try using hard-coded strings you will see it works:
function read_c3d_feat(output_list_relative)
dir_list = {'21';'45';'18'};
for i = 1:size(dir_list, 1)
dir_str = dir_list{i}; % Loops through '21','45','18'
% The dir function now works because we know dir_str is a valid string
feat_files = dir([dir_str, '/*.res5b']);
end
end
This means you need to debug your code and find out what this line is actually assigning to dir_list:
dir_list = importdata(output_list_relative);
Note that if dir_list is a cell of text entries, you should be indexing it with curly braces as above. If instead it is a matrix (because all of the entries seem to be numerical anyway) then you should be using num2str when passing to dir:
function read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1:size(dir_list, 1)
feat_files = dir([num2str(dir_list(i)), '/*.res5b']);
% ...

Variable not defined

I wanted to run this line but unfortunately it throws an error. Any ideas?
Undefined function or variable mr.
Error in Playground (line 23)
X = min(mr);
j = 1;
for i = 1:(resolution1+1)
line(i) = m(a(1))*ab(i)+c;
end
for i = 1:(resolution1)
if or(or(line(i)>ab_y(i) & line(i+1)<ab_y1(i+1),line(i)<ab_y1(i)& line(i+1)>ab_y1(i+1)),line(i)==ab_y1(i))
mr(j) = ab1(i);
rk(j) = ab_y1(j);
j = j+1;
end
end
X = min(mr);
Y = max(mr);
Your condition:
or(or(line(i)>ab_y(i) & line(i+1)<ab_y1(i+1),line(i)<ab_y1(i)& line(i+1)>ab_y1(i+1)),line(i)==ab_y1(i))
returns always false, so the statements inside
mr(j)=ab1(i); rk(j)=ab_y1(j);
are never executed. Therefore variable mr does not exist.
Add an mr = []; statement before the loop to initialize the variable (and also revise your condition why it returns always false).

Input argument "b" is undefined

i am new in matlab and search everything. I am writing a the function. i could not able to understand why this error is comning :"Input argument "b" is undefined." . shall i intialise b =0 ? whereas it is the parameter coming from input console. my code:
function f = evenorodd( b )
%UNTITLED2 Summary of this function goes here
%zohaib
% Detailed explanation goes here
%f = b;%2;
f = [0 0];
f = rem(b,2);
if f == 0
disp(b+ 'is even')
else
disp(b+ 'is odd')
end
console:
??? Input argument "b" is undefined.
Error in ==> evenorodd at 6
f = rem(b,2);
From what I see, this is what you are trying to do:
function f = evenorodd( b )
f = rem(b,2);
if f == 0
fprintf('%i is even\n', b)
else
fprintf('%i is odd\n', b)
end
=======================
>> evenorodd(2);
2 is even
No need to initialize f as [0,0].
In MATLAB, you cant concatenate a number and string with + operator. Use fprintf.
The above function evenorodd takes one argument (integer) and returns 0 or 1.

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1

Input argument "ip" is undefined Error

I am new in matlab I am trying to define a function and I keep getting this error
" input argument "ip" is undefined.
Error in ==> edge_mapping at 2
size_ip = size(ip(:,:,1)); "
here is my code
function[op1,op2,op3] = edge_mapping(ip)
size_ip = size(ip(:,:,1));
s=size_ip(1);
op1= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op2= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op3= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
for i = 1 : 10
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
function [op1, op2, op3] = edge_mapping(ip)
op1 = zeros(size(ip));
op2 = zeros(size(ip));
op3 = zeros(size(ip));
for i = 1 : size(ip, 3)
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
return
But I think it's better to write a simple function:
function op = edge_mapping(ip, edge_mode)
op = zeros(size(ip));
for i = 1 : size(ip, 3)
op(:,:,i)=edge(ip(:,:,i), edge_mode);
end
return
and then call it:
op_sobel = edge_mapping(ip, 'sobel');
op_prewitt = edge_mapping(ip, 'prewitt');
op_canny = edge_mapping(ip, 'canny');
This code is a function. It has to be save as m-file and run from MATLAB command line, script or another function as
[op1,op2,op3] = edge_mapping(ip);
where arguments ip, op1, op2, and op3 can have different names.
Make sure you have space after function keyword.
To avoid this error you can assign a default value for the input argument if it's undefined (not exist in the function's scope):
if ~exist(ip, 'var')
ip = []; %# or other default value
end