Class properties Value not available in a different function.

1 view (last 30 days)
I have a class called BatteryPlotCalculation and one of its properties is BatteryVoltageSection.
properties
BatteryVoltageSection
end
Then there is a method that sets the values for BatteryVoltageSection.
function plotCalculations(obj)
obj. BatteryVoltageSection %This is the last line of the function and I removed semicolon to see what
% value is coming here and it works
end
Then there is a function to return this value
function Battery_Voltage_Section = getSectionValues(obj)
obj. BatteryVoltageSection % I tried the same here, i.e remove semicolon to see what values are here,
% but here it is empty.
Battery_Voltage_Section = obj.BatteryVoltageSection;
end
So why is it, why is the value available on the top, but not in the second function. They are both the same value. Also in the main program, they are called in the correct order as well.
BatteryPlotValues = BatteryPlotCalculation(app.MeasurementData,app.DataType, app.ReferenceVoltage, app.ReferenceEnergy, MaxMinArray);
BatteryPlotValues.plotCalculations;
BatteryVoltageSection = BatteryPlotValues.getSectionValues;
So this is how they are called in the main program. So you can be sure that second function is only called after the first function has been called, which means the values obtained in the first function for obj.BatteryVoltageSection should be available in the second function as well. But it is not there. Why is that? Thank you.

Answers (2)

Ayush
Ayush on 22 Jul 2024
Edited: Ayush on 22 Jul 2024
Hello Govind,
You can consider some of the debugging steps to ensure that your code and specifically the value of obj.BatteryVoltageSection is not being modified in the code flow:
1. Check for initialization of BatteryVoltageSection and if it is in the constructor or before any method uses it.
2. Verify if the value set by the function plotCalculations is correct and within the scope of the class to be accessed by other functions as well.
3. Try leveraging MATLAB's debugging tools to inspect the code at various parts by adding breakpoints and print statements along with incremental step-wise running of the code with Live Editor. You can further refer to the below documentation links for your reference:
Hope this would help you understand the change in value between the two functions.

Aneela
Aneela on 22 Jul 2024
Hi Govind,
I have tried this modified code in R2024a and it worked fine for me:
classdef BatteryPlotCalculation
properties
BatteryVoltageSection;
end
methods
function obj=plotCalculations(obj)
% set BatteryVoltageSection
obj.BatteryVoltageSection = [1, 2, 3, 4];
disp(obj.BatteryVoltageSection);
end
function Battery_Voltage_Section = getSectionValues(obj)
disp(obj.BatteryVoltageSection); % Display to verify the value
Battery_Voltage_Section = obj.BatteryVoltageSection;
end
end
end
The function definition of “plotCalculations” should be modified to return the object, since the class property is being updated.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!