Class properties Value not available in a different function.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Govind Sankar Madhavan Pillai Ramachandran Nair
el 22 de Jul. de 2024
Respondida: Aneela
el 22 de Jul. de 2024
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.
0 comentarios
Respuestas (2)
Ayush
el 22 de Jul. de 2024
Editada: Ayush
el 22 de Jul. de 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:
Variable value tracking: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#mw_706ee310-0453-49d3-a675-30c4c242b844
Analysis tools for debugging: https://www.mathworks.com/help/matlab/debugging-code.html
Hope this would help you understand the change in value between the two functions.
0 comentarios
Aneela
el 22 de Jul. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!