I'm learning Computer Vision(& new to Octave/Matlab) and wrote this code in Octave:
function result = func_scale(img, value)
result = value .* img;
endfunction
husky = imread('Husky.jpg');
imshow(func_scale(husky, 1.5));
On running the file I'm getting this error:
error: 'value' undefined near line 3 column 12 error: called from
func_scale at line 3 column 10
I have named the file
func_scale.m
Any idea what I'm doing wrong?
Thanks.
If you create a file func_scale.m with the content
function result = func_scale(img, value)
result = value .* img;
endfunction
and you call it from octave with
func_scale
obviously the parameter img and value are missing for the calculation in line 2.
To do what you want you can leave your func_scale function in the file func_scale.m but move the call (imread... imshow) to another file, for example myfile.m. You can then call this from Octave with myfile
Or create one file foobar.m which starts with 1;, then your function definition and then your function call
Related
I have the following MATLAB function:
function getDBLfileL1(pathInput,Name_file,folderName)
DBL_files=dir([pathInput,'/*.DBL']); %get DBL files
fprintf('Reading DBL files ... ')
for i = 1:length(DBL_files) %loop through all DBL files
[HDR, CS]=Cryo_L1b_read([pathInput,'/',DBL_files(i).name]); %read data with ESA's Cryo_L1_read function
Coord{i}.LAT_20Hz=CS.GEO.LAT; %store values to struct
Coord{i}.LON_20Hz=CS.GEO.LON;
Coord{i}.BoundingBox_StartLATLON_StopLATLON=[HDR.START_LAT*10^-6,HDR.START_LONG*10^-6,HDR.STOP_LAT*10^-6,HDR.STOP_LONG*10^-6];
Coord{i}.FileName=[pathInput,'/',DBL_files(i).name];
end
eval([Name_file '= Coord;']);
save(['output/',folderName,'/',Name_file,'.mat'],Name_file,'-v7.3')
fprintf('done\n')
end
And it is called in the following:
getDBLfileL1(pathInput,[folderNames{i},'_',folderNames1{j}],folderNames{i}); %read Data from DBL file
The Value of Name_file is '2011_01', and I get the following error:
eval([Name_file])
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
Does anyone know why this error occur or how I can change the file, that I can replace the eval() function?
Thanks a lot in advance!
If I got it right, you are trying to evaluate '2011_01= Coord;' , which means that you are assigning Coord into a variable called 2011_01, and variable names cannot start with numbers
For debugging purpose, I want to print out the name of the m-file and the line number so that, when something goes off, it says something like
Error at 197th line of run.m
Given that Matlab displays an error message of this type, there must be a solution.
How can I do this?
===========Follow-up question==============
Thank you! But I got a follow-up question.
I have main.m which is the main m-file and main.m calls lower.m
I wrote
info = dbstack()
and
try
error('Toto')
catch ME
ME.stack
end
to the main.m. Then Matlab displays
info =
file: 'main.m'
name: 'main'
line: 15
ans =
file: 'C:\Users\account1\Google Drive\1AAA-RA\11HHJK(O…'
name: 'main'
line: 18
However, when I write dbstack and try-catch thing to lower.m, then Matlab does not display any exact information.
I ran the main.m and main.m called the lower level m-file but it doesn't display any information.
info =
2x1 struct array with fields:
file
name
line
ans =
2x1 struct array with fields:
file
name
line
What should I do to make it display every file, name, line information?
Ideally, Matlab will display the information of both main.m and lower.m like the following:
file1: 'main.m'
name1: 'main'
line1: 15
file2: 'lower.m'
name2: 'lower'
line2: 6
Is this possible?
You can use the Matlab Exception object. The line information is contained in the 'stack' property.
Example create a 'test.m' script:
try
error('Toto')
catch ME
ME.stack
end
Output of command window is:
>> test
ans =
file: 'D:\MATLAB\Sandboxes\AVLab\test.m'
name: 'test'
line: 2
Or without a try/catch you can use MException.last.
See documentation link http://www.mathworks.com/help/matlab/matlab_prog/capture-information-about-errors.html
info = dbstack();
% info is now a struct that has 3 fields
% info.file
% is a string containing the file name in which the function appears.
% info.name
% is a string containing the name of the function within the file.
% info.line
% is the line number on which dbstack was called
MATLAB's error function will automatically display the filename and line number.
function [ ] = wsgefiow9ey( )
n = 7;
if n > 1
msg = 'Error: n is too big.';
error(msg)
end % if
end % function definition
click here to see console output resulting from call to MATLAB error function
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.
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.
This is my function
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
and I called
[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1])
??? Undefined function or method 'stat' for input arguments of type 'double'.
I also tried
mean,stdev = stat([12.7 45.4 98.9 26.6 53/1])
??? Input argument "x" is undefined.
Error in ==> mean at 30
y = sum(x,dim)/size(x,dim);
Both of them are wrong and I cannot figure out why.
Could you please help me =] Thanks a lot
Your function looks fine to me, therefore I assume that your Matlab "Current Directory" is not the same directory where your function lives.
Another reason might be that the file in which this function exists does not have the same as this function. In order for Matlab to know that this function exists, it has to live in a separate file called stat.m (note how file name is the same as the function name).