Invalid expression error when I try to introduce new variable.

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 Comments

Please show us lines 400 to 410
dbtype ImageData 400:410
dbtype Imagedata 400:410
400 % 'hours' and 'frames'.
401 %
402 % Property/Value inputs:
403 % Margins - Two element array with margins to the left and
404 % right of the plotted time interval, given as
405 % fractions of the plotted time interval. As an
406 % example [0.01 0.05] gives a 1% margin to the left
407 % and a 5% margin to the right. The default is [0 0].
408
409 % Parse property/value inputs:
410 aMargins = GetArgs({'Margins'}, {[0 0]}, true, varargin);
>>
@Walter Roberson Could you please explain, why you asking for these specific lines of Image data?
@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.
Oh, right, ImageParameters not ImageData
@Walter Roberson Yeah, that's what I thought:)
Here it is!
40
0 channelIndex = find(strcmp(this.channelNames, aChannel));
401 end
402
403 oTrans = all(this.channelColors{channelIndex} ==...
404 this.channelColors{channelIndex}(1));
405 end
406
407 function oMicrons = PixelToMicroM(this, aPixels)
408 % Converts a distance from pixels to micrometers.
409 %
410 % Inputs:
I am going to guess that an end is missing
Where exactly? Sorry, didn’t understand.
It works all perfectly fine until I introduce scale = 11.2. Then it shows warning on 11.2 that it’s invalid, and when I run - gives the error.
Where exactly are you putting that assignment?
@Walter Roberson , at the line 407.
396 % Get the index of the channel.
397 if isnumeric(aChannel)
398 channelIndex = aChannel;
399 else
400 channelIndex = find(strcmp(this.channelNames, aChannel));
401 end
402
403 oTrans = all(this.channelColors{channelIndex} ==...
404 this.channelColors{channelIndex}(1));
405 end
406
407 scale = 11.2;
408
409 function oMicrons = PixelToMicroM(scale, aPixels)
410 % Converts a distance from pixels to micrometers.
411 %
412 % Inputs:
413 % aPixels - A distance or an array of distances in pixels.
414 %
415 % Outputs:
416 % oMicrons - A distance or an array of distances in
417 % micrometers.
418
419 oMicrons = aPixels * scale;
420 end

Sign in to comment.

Answers (2)

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 Comment

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)

Sign in to comment.

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.

1 Comment

@Steven Lord That's true: it;s not inside any functions, it;s betweeb them.
I plan to use this variable in couple of functions. And one of the functions starts on line 409.
This is what it was at the begining:
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
And instead of this.xCalibrationMicrons I want to use scale.
Thank you so much for your help!

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 21 Sep 2021

Commented:

on 21 Sep 2021

Community Treasure Hunt

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

Start Hunting!