Invalid expression error when I try to introduce new variable.
Afficher commentaires plus anciens
I have a code with multiple functions, and at one place I wrote (which I later use in multiple functions, multiplying values or dividing)
scale = 11.2;
It gives this error
Error using ImageData
File: ImageParameters.m Line: 407 Column: 17
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
I don't understand why.
Without this everything works great!
Some additional data for the question:
Originally file describes class, with one of the properties
xCalibrationMicrons = []; % The image pixel width in microns.
this.xCalibrationMicrons =...
this.Get('pixelSize') / this.Get('magnification');
Example of function I want to change
function Microns = PixelToMicroM(this, aPixels)
% Converts a distance from pixels to micrometers.
%
% Inputs:
% aPixels - A distance or an array of distances in pixels.
%
% Outputs:
% oMicrons - A distance or an array of distances in
% micrometers.
Microns = aPixels * this.xCalibrationMicrons;
end
Basically, I want to use scale instead of this.xCalibrationMicrons.
Thank you very much!
10 commentaires
Walter Roberson
le 21 Sep 2021
Please show us lines 400 to 410
dbtype ImageData 400:410
CheshireM
le 21 Sep 2021
@MaKo I think Walter meant
dbtype ImageParameters.m 400:410
Because your error message reports an error in line 407 of ImageParameters, we should naturally look at that part of the code.
Walter Roberson
le 21 Sep 2021
Oh, right, ImageParameters not ImageData
CheshireM
le 21 Sep 2021
Walter Roberson
le 21 Sep 2021
I am going to guess that an end is missing
CheshireM
le 21 Sep 2021
Walter Roberson
le 21 Sep 2021
Where exactly are you putting that assignment?
CheshireM
le 21 Sep 2021
Réponses (2)
Walter Roberson
le 21 Sep 2021
You cannot just define a constant inside a class by assigning to it, and expect it to be available to your functions.
You can create a class property to hold the scale, and you could mark it as a constant.
However, you should still need a class or object prefix to refer to it, so instead of this.xCalibrationMicrons it might look like this.scale . I am not sure that is what you were looking for: it seems to me you were hoping to define a short name without any prefix .
An approach that might work:
if you were to define a class method named scale that is marked as static, and which fetches a class variable that has the appropriate value, then you could, I think, write
Microns = aPixels * scale;
in which case scale might look like a simple variable, but it is really a static method being invoked with no parameters, equivalent to
Microns = aPixels * scale();
1 commentaire
Steven Lord
le 21 Sep 2021
Static class methods need to be invoked using the class name. If you're going to do that to just return a constant, I'd make a Constant property instead.
If you only wanted this value to be accessible to functions and methods inside this class you could define a class-related function named scale and call that. As an example copy the code from the next block into a file:
classdef example1457279
properties
x;
end
methods
function obj = example1457279(xin)
obj.x = xin;
end
function q = scaleInput(obj)
q = obj.x * scale();
end
end
end
function y = scale
y = 2;
end
Now call it:
b = example1457279(1:10);
c = scaleInput(b)
Steven Lord
le 21 Sep 2021
0 votes
It's likely (in the absence of nested functions) that the statement on line 407 is not inside any function. The end on line 405 ends the previous function and the function on line 409 starts the next one. So that would be unreachable code anyway if MATLAB didn't just throw an error.
Where are you planning to use that variable? That would inform where the variable should be defined.
Catégories
En savoir plus sur Images dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!