Invalid expression error when I try to introduce new variable.

1 vue (au cours des 30 derniers jours)
CheshireM
CheshireM le 21 Sep 2021
Commenté : Steven Lord le 21 Sep 2021
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
Walter Roberson le 21 Sep 2021
Where exactly are you putting that assignment?
CheshireM
CheshireM le 21 Sep 2021
@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

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
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
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)

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 21 Sep 2021
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 commentaire
CheshireM
CheshireM le 21 Sep 2021
@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!

Connectez-vous pour commenter.

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by