What is meant by "too many output arguments"? - matlab

I run this code, but I have an error in "error function".
The error message I get it:
Error using error
Too many output arguments.
Does anyone know what is the problem?
w=2*rand(3,4)-1; % Randomly choosen between -1 and 1
x=[ 5 55 14 32; 4 4 84 5; 4 31 9 4; 4 45 99 2]; % Desired outputs
d=[ 1, 1, 0 ; 0, 1, 0 ; 0, 0, 1 ];
for j1=1:3,
yi=w'*x(:,j1); % Network output
y=sign(yi);
if sum(y-d(1,:)') > 0
error = error+1
end
end

The variable error is not defined and is used before defining it. As it is not defined.....it is taking the inbuilt matlab function error. Don't use the variable names as MATLAB inbuilt funcitons. You rename this variable as some other variable name, say myerror. Don't forget to initialize it. Check the below example code.
for i = 1:10
error = error+1 ;
end
The above code shows error Too many output arguments. Because the code takes error is inbuilt function, as it is not initialized.
error = 0 ;
for i = 1:10
error = error+1 ;
end
The above works, as we have initialized error, code will not take the inbuilt function.
But the above is not suggested. Never overwrite the existing functions in matlab as variable names. The below is suggested.
myerror = 0 ;
for i = 1:10
myerror = myerror+1 ;
end

I suspect that the source of your problem is because error is a MATLAB function and you try to use it as a variable, hence the error message "Error using error". Change your variable name to something like my_error or similar, and it should work.

Related

Error running matlab code after compiling

It looks like this has been asked many times, but none of the past posts seem to solve my question. All those had to do with matrix/vector while my code does not have any of these, just simple variables. It takes three variables as arguments. It works perfectly fine within the Matlab environment. I only got the error when I compiled it with mcc -m Normal.m and tried to run with the executable like this "./Normal 1 5 0.5". The complete error message is:
Error using /
Matrix dimensions must agree.
Error in Normal (line 4)
MATLAB:dimagree
It is complaining about line 4: N=2/dt, what is wrong with this?
Here is the code:
function val=Normal(l1,l2,dt)
const=(l2/l1-1);
N=2/dt;
S=1.0/sqrt(l2/l1);
Z(1)=S;
for i=2:N
t= -1+(i-1)*dt;
Z(i)=1.0/sqrt(const*t*t+1);
S=S+2*Z(i);
end
Z(21)=1.0/(l2/l1);
S=S+1.0/sqrt(l2/l1);
val=dt*S/2;
end
But dt is not a scalar when passed into the standalone through the command ./Normal 1 5 0.5. It is a character array with 3 elements ('0', '.','5')!
When passing numerical arguments to a standalone, they are passed as strings. Thus, inside the function, you need to convert '0.5' into a double, and similarly for l1 and l2:
dt = str2num(dt);
l1 = str2num(l1);
l2 = str2num(l2);
Note that you can use isdeployed to determine at runtime if the function is a standalone:
if isdeployed, dt = str2num(dt); end
And you might need to display the result:
if isdeployed, disp(val); end
Result:
>> system('Normal 1 5 0.5');
1.4307
>> Normal(1,5,0.5) % .m function for comparison
ans =
1.4307

Matlab says 'find' is not defined in a function [duplicate]

This question already has an answer here:
MATLAB error: "previously appeared to be used as a function or command"
(1 answer)
Closed 8 years ago.
In my command window, I can execute find([0 1 0]), but when I run find in a function, as in x = find([0 1 0]), the compiler tells me that find isn't defined. Why might that be?
The error is:
??? Error: File: frequentTuples.m Line: 12 Column: 21
"find" previously appeared to be used as a function or command, conflicting with its
use here as the name of a variable.
A possible cause of this error is that you forgot to initialize the
variable, or you have initialized it implicitly using load or eval.
and here's the code. The error occurs on the second line of the for loop.
function [ tuples ] = frequentTuples( k, candidates, transactions, min_support )
%FREQUENTTUPLES Get frequent itemsets of size k
% Detailed explanation goes here
candidate_tuple_is_frequent = zeros(size(candidates, 1));
for i = 1:size(candidates, 1)
columns_of_candidate_items = transactions(:, candidates(i, :));
indices_of_transactions_containing_all_items = find(sum(columns_of_candidate_items') == k);
candidate_tuple_is_frequent(i) = size(indices_of_transactions_containing_all_items) >= min_support;
end
tuples = candidates(find(candidate_tuple_is_frequent, :));
end
Ah, I see your problem now. You have a misplaced bracket on line 13. You have
tuples = candidates(find(candidate_tuple_is_frequent, :));
When you should have
tuples = candidates(find(candidate_tuple_is_frequent), :);
You're trying to call find(candidate_tuple_is_frequent, :), which is trying to treat find as a variable. This means that any other call to find in the function will treat it as a variable, hence your error.

'Undefined function or variable' in Matlab

Here is my code:
function [im,sindx,end1]=alln(im,i,j,secret,sindx,end1)
slen=length(secret);
p=im(i,j);
neigh= [im(i-1,j) im(i+1,j) im(i,j-1) im(i,j+1) im(i-1,j-1) im(i+1,j-1) im(i-1,j+1) im(i+1,j+1)];
minpix = min (neigh)
maxpix = max (neigh)
if minpix < p < maxpix
lowlim = minpix+1;
highlim = maxpix-1;
range = highlim-lowlim+1;
nbits=floor(log2(abs(range)));
if sindx+nbits-1>slen
end1=1;
return
end
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
b = bin2dec(bin);
newvalue1 = abs (minpix + b);
newvalue2 = abs (maxpix - b);
if abs(p-newvalue1)<= abs(p-newvalue2)
im(i,j) = newvalue1;
else
im(i,j) = newvalue2;
end
sindx=sindx+nbits;
end
end
My main program calls this function. When I run the program, I get the following error message:
??? Undefined function or variable "bin".
Error in ==> alln at 34
b = bin2dec(bin);
I know there are many experts for whom this is not a problem at all. I am new to MATLAB. Please guys, show me the way, which type of modification in the code can overcome this problem?
First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that
b = bin2dec(bin);
is line 34, but it's line 22 in the code you present.
OK, that aside...
The error message says that 'bin' isn't defined, but I see that it's being set on the line...
bin(k)=secret(sindx+k-1);
That suggests to me that THAT line isn't being run.
I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.
Try removing the semicolon from the end of the
nbits=floor(log2(abs(range)));
line and run your code again.
Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.
At what input arguments to the function alln, are you getting the error?
Lets suppose that nbits is 0, then the following loop will not run:
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.

Variable variables in Matlab

I have 30 txt files with data
And I want to create on the fly vectors from that files with the name of "file name"
pathforindependents = 'C:\MatLab\independent\'
independents = dir(fullfile(pathforindependents,'ind*.txt'))
for i = 1:length(independents)
filename = independents(i).name;
r=regexp(filename,'\.','split');
qnumber = r(2)
qtitle=r(3)
qpath = strcat(pathforindependents,filename)
qdata = load(qpath)
mtrxPrefix = 'mtrx_';
v = strcat(mtrxPrefix,qtitle);
eval(???????????????????????)
end
But I dont know how can I do it. No matter what I try Matlab gives me "Undefined function 'eval' for input arguments of type 'cell'." Error?
My data file structure is like
ind.01.AGE.txt
0
1
0
0
0
1
1
0
1
...
At the end I want to reach this
mtrx_AGE =
0
1
0
0
0
1
1
0
1
...
How can I do it ? Thank you.
To put the variables in the base workspace, use assignin:
assignin('base', v, qdata);
As you can see in the assignin documentation, for certain assignment cases you may want to use evalin.
you can use fields within structures with sprintf to name variables on the fly:
for i = 1:100
my_struct.(sprintf('A%s%i','filename',i)) = i^2
end
would make
my_struct.Afilename1 = 1
my_struct.Afilename2 = 4
my_struct.Afilename3 = 9
Read Mathworks TechNote 1103 on why you should avoid using EVAL the way you do. Alternatives include cell arrays or structures.

MATLAB error - ??? Attempt to reference field of non-structure array

I'm writing an insertion sort in MATLAB. I called my function like this:
>> A = [5 4 3 2 1]
A =
5 4 3 2 1
>> insertion_sort(A)
but when I run it I get the error
??? Attempt to reference field of non-structure array.
Error in ==> insertion_sort at 6
for j=2:original.length
Here's my original code:
function sorted = insertion_sort(original)
for j=2:original.length
key = original(j);
i = j-1;
while i > 0 && original(i) > key
original(i+1) = original(i);
i = i-1;
end
original(i+1) = key;
end
sorted = original;
end
Anyone know what I'm doing wrong?
Try numel(original) instead of original.length. MatLab matrices are primitive types, not objects, and they don't have a length property.
You want to use numel(original) instead of original.length. Fundamental data types don't have a length method, so MATLAB mistakenly thinks you are trying to access a field named length in a structure, which original is not.