MATLAB Answers

Invalid expression error when I try to introduce new variable.

조회 수: 60(최근 30일)
CheshireM
CheshireM 2021년 9월 21일 0:19
댓글: Steven Lord 2021년 9월 21일 20:11
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
CheshireM
CheshireM 2021년 9월 21일 13:59
@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

댓글을 달려면 로그인하십시오.

답변(2개)

Walter Roberson
Walter Roberson 2021년 9월 21일 14:44
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
Steven Lord
Steven Lord 2021년 9월 21일 20:11
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
Steven Lord 2021년 9월 21일 14:11
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
CheshireM
CheshireM 2021년 9월 21일 14:19
@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!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by