Unrecognized function or variable 'size'. in function called from script

The Problem:
I have a script, where I call two self-written functions. One of the functions is called weightingFkt2(). In this function I call the find() function of Matlab, which returns indexes. Now, if I want to call indexsize=size(index,2) I get an error "Unrecognized function or variable 'size'." while running the code.
What I have already done (besides researching):
I checked in the weightedFkt(), if it is a matrix. It is.
If I write the same line (indexsize=size(index,2);) in the command window after getting the error (I told matlab to pause on errors) it works fine.
I got the same error on different variables in the same function before. There I was able to put the call of size() into the script, where it worked fine.
I use Matlab R2019b.
The relevant lines of code in the functioin I attach, the line where I get the error is line 43, the lines where I got errors, but was able to put the line in the script are 5,6 and 17.
In the script the relevant lines of codes are:
origsize1=size(origimage,1);
origsize2=size(origimage,2);
newsize1=round(origsize1*1.25);
newsize2=round(origsize2*1.25);
for x=1:newsize1
for y=1:newsize2
%findnearestindeces: finds the nearest points in valuepixels
%of x and y
nearestindex=findnearestindeces(x,y,valuepixels(:,:,:)); %valuepixels: valuepixels(s,t,:)=[currentPoint(1), currentPoint(2), neworigimage(s,t)];
size3=size(nearestindex,1); %due to error message in weightingFkt() now in script
cp=[x y];
imagevalue=weightingFkt2(nearestindex, cp, valuepixels, size3);
finalimage(x,y)=imagevalue;
end
end
So my question is, how I can solve that.
As that is my first question in this forum I tried to follow all the guidelines and add all the relevant code but please let me know, if something is missing.

2 件のコメント

Walter Roberson
Walter Roberson 2021 年 8 月 18 日
I wonder if the error is against nearestindex instead of against size? If length1 < 1 then you never assign to nearestindex
Katharina Heitger
Katharina Heitger 2021 年 8 月 18 日
Thank you for your answer.
length1 is 4, since nearestindex is of 4x2 double.
I already asked myself a similar question, not that nearestindex is not assigned, but I checked and looked. All of the 8 values (4x2) are double and it is a matrix. In fact they have values like ([412 1]; [1 1]; [514 1]; [413 1]).
And the find() function returns a value, in this case a 1x1 double, but nevertheless.
And everytime I get this error, and I run the size() command int the command window I works out fine.

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 19 日
Near the bottom of the script you have
size=size(nearestindex);
Because you assign to the variable size, MATLAB assumes that size must be a variable everywhere in the script.
Do not use the same name as both a function and a variable: MATLAB is permitted to make these kinds of assumptions now.

1 件のコメント

Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
Thank you! Now I'm really embarassed... I double or triple checked the code, with my eyes and with who. But that I overlooked. Thank you again!

サインインしてコメントする。

その他の回答 (1 件)

Chunru
Chunru 2021 年 8 月 18 日
編集済み: Chunru 2021 年 8 月 18 日

1 投票

Most likely, somewhere in your code you use "size" as a varibale name. Try "debstop error"; When error occurs, use "whos" to see if "size" is in the variable list.

8 件のコメント

Katharina Heitger
Katharina Heitger 2021 年 8 月 18 日
Thank yu for your answer.
I always use something like newsize or size1 to avoid that. But nevertheless I now double-checked that. Thats not the case.
Chunru
Chunru 2021 年 8 月 18 日
Provide more details of the error messages (where it goes wrong). It's even better if you can provide executable code that demonstrate the error.
Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
The whole error message is:
Unrecognized function or variable 'size'.
Error in weightingFkt (line 52)
indexsize=size(index,2);
Error in finalSkript4v1 (line 125)
imagevalue=weightingFkt(nearestindex, neworigimage, cp, valuepixels, size3);
I have to admit, that the weightingFkt2 I provided earlier is the relevant part of weightingFkt(), so I created a shorter version of this function for this question.
The executable code is comming soon.
Walter Roberson
Walter Roberson 2021 年 8 月 19 日
編集済み: Walter Roberson 2021 年 8 月 19 日
In the code, just before the error, try
which size(index,2)
also, I suggest taking the code and copying the function name directly from the code (using your mouse), and then at the command window do
'size'+0
except copying from the code into the command. The purpose of this would be to check that there are no special characters, no mistaken spelling that is being overlooked
Question: does your code just happen to assign to a variable named path ? If so, then Don't Do That.
Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
Thank you for the answer.
You mean, that I have a variable 'path'? No, I don't have that.
I try the which command.
You mean, that I copy the function name to the command window, so that I run the function from the command window and while it is running do 'size'+0? Entering something while running is possible?
Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
Here is the code. I shortened the finalSkript, where it takes an already finished calculation and creates valuepixels. I saved valuepixels in a .mat-file.
Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
You said, I should copy the function name to the command window. Do you mean the name of my self-written fucntion or the size()-function? The size()-function works via the command window fine.
Output of the which command, so the path of the function is:
built-in (C:\Program Files\MATLAB\R2019b\toolbox\matlab\elmat\@double\size) % double method
In my opinion this looks fine, but maybe it isn't?
Katharina Heitger
Katharina Heitger 2021 年 8 月 19 日
So I copied the name of the self-written function into the command window and had a breakpoint right before the error occurs. Here I entered 'size'+0 into the command window, where the ouput was:
ans =
115 105 122 101
And the the error occured. But I don't think, that this was what you meant.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by