i have a function
function[a,b,c] = segmentimage(image)
i am not able to give the image 'bird.png' to this function.
i get an error if i write
segmentimage('bird.png')
as
Error: File: segmentimage.m Line 27 Column: 66
if i declare
image= imread(bird.png)
before the function declaration, then also i am getting an error
Function definitions are not permitted at the prompt or in scripts.
please help. thanks in advance
In option 2 I think you're doing something like this in your script,
image= imread(bird.png)
function[a,b,c] = segmentimage(image)
First of all, you should have
image = imread('bird.png');
and you should not declare anything in the function script, so, erase the first line.
it should be,
function[a,b,c] = segmentimage(image)
%your code here,
im = imread(image);
%a = ...
%b = ...
%c = ...
end
then you can call your function in another script,
[a, b, c] = segmentimage('bird.png');
About the first error, you have to post some code, especially the line in which the error occurs so we can help.
Related
I want to find the Minimum of a function using
[x,fval] = fminsearch(#(param) esm6(param,identi),result(k,1:end-1),options)
now for each Iteration step i want some values that the function 'esm6' calculates to be saved in an Array. I tried the following:
In the first line of the function i wrote
identi.sim.i_optiIter = identi.sim.i_optiIter + 1;
to have an iteration-variable counting the iteration steps of fminsearch. And later to catch the values that I need I used
identi.sim.guete_werte.gew(identi.sim.i_optiIter,:,:) = y_sim;
identi.sim.guete_werte.ungew(identi.sim.i_optiIter,:,:) = y_sim_ungew;
and to make sure that I use the new values of the identi-struct for the next function call, I wrote this at the end of the function:
assignin('base','identi',identi);
Now unfortunatly it doesn't do what I wanted it to do. Can anyone help me with this?
EDIT:
I made another attempt on it, using an Output function. I extendend my Options like this:
options = optimset('Display','iter','MaxIter',3,'OutputFcn',#outfun);
But now the Problem is that i cannot figure out where to put this outfun. The outfun Looks like this:
function stop = outfun(x,optimvalues,state,iteration,y_sim,y_sim_ungew)
stop = false;
if state == 'iter'
guete_werte.gew(iteration,:,:) = y_sim;
guete_werte.ungew(iteration,:,:) = y_sim_ungew;
end
end
Now the Problem with it is, that i can not put it in the file, where i call the fminsearch, because that is a script. If i put the outputfunction into a separate .m-function file, it is not able to Access the variables of the esm6 function. And if I add it to the esm6-function file, matlab can't find the function and says
??? Error using ==> feval Undefined function or method 'outfun' for
input arguments of type 'struct'.
I am trying to call Eviews (8, 32bit) from Matlab (2013a, 32bit), but without success so far. I used the following code in Matlab:
clear all; clc;
hm = actxserver('EViews.Manager.8')
hm = COM.Eviews_Manager
h = hm.GetApplication(0)
h = Interface.EViews_8.0_Type_Library.IApplication
h.invoke
h.Show()
h.Run('wfcreate comEV u 4')
h.Run('genr x = #obsid')
I get the following error:
>> h = hm.GetApplication(0)
No appropriate method, property, or field GetApplication for class
COM.EViews_Manager_8.
Could anyone please help? Thanks in advance.
At the very least, you could write your program in EViews and then call it from Matlab* using the shell:
system(['"C:\Program Files\EViews 9\EViews9_x64.exe" "C:\Users\me\Documents\foo.prg"'])
*or Stata, R, Python, VBA, etc.
The line of code
h = Interface.EViews_8.0_Type_Library.IApplication
is not a command, is the output from the above line of your code
(h = hm.GetApplication(0)).
Just remove it from your script. I think this is the problem.
I'm having some trouble with local functions within my code so I've pasted a simple example below:
function [avg,testvar] = test(x) %Warning
n = length(x);
avg = mymean(x,n);
end
function [a,testvar] = mymean(v,n)
a = sum(v)/n;
testvar=123;
end
One can probably see what I'm attempting; to pass testvar out of the local functions. However Matlab returns the warning:
"The function return value 'testvar' might be unset"
with respect to the line I've commented "%Warning".
What's the best way of getting around this?
You need to specify the value of the second output of test(). Otherwise how can MATLAB know what its value is supposed to be? It doesn't know the second output of mymean() should be routed to the second output of test(). Perhaps this will solve your problem.
function [avg,testvar] = test(x) %Warning
n = length(x);
[avg, testvar] = mymean(x,n);
end
function [a,testvar] = mymean(v,n)
a = sum(v)/n;
testvar=123;
end
The variables between brackets after function are the output variables.
In your first function, you did not assign any value to testvar hence the warning. If you add testvar = 123; in the first function, the warning goes away. Or you can remove testvar from the output variables, leaving:
function avg = test(x)
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.
Assume I have the function:
function name_the_paramlist(varargin)
% Print out varargin exactly how it is called as a string
Basically what I want it to do is, calling:
name_the_paramlist({'x', x}, y, 1, 'hello', [1, 2; 3, 4])
should print to the screen the string:
'{''x'', x}, y, 1, ''hello'', [1, 2; 3, 4]}'
Any suggestion?
ETA: The reason I want something like this is to hopefully resolve this question:
Matlab - how to create a subclass of dataset class keeping the dataset parameter constructor
Well, if you type this function call in a command line or run with F9 (so it get saved in the history) you can read the history.m file and get the last command as a string.
fid = fopen(fullfile(prefdir,'history.m'),'rt');
while ~feof(fid)
lastcmd = fgetl(fid);
end
fclose(fid);
Then get the argument part:
arg_str = regexp(lastcmd, '\w+\((.+)\)','tokens','once');
arg_str = strrep(arg_str{:},'''','''''');
UPDATE:
Another idea. If you call this function from another script or function (m-file) you can use DBSTACK to return the m-file name and the current line number:
SI = dbstack;
filename = SI(end).file;
lineNo = SI(end).line;
Then you can follow similar technique as in the first solution to read that line from the m-file and get argument part.
In opposite to the first solution this won't work if run from command line or editor cell mode.