Accessing table variables in a class method
Show older comments
I have some tabular data with variable names for the columns in my main file. I am passing the tables as variables into methods in a class. In those methods, I wish to be able to access and edit the tabular data according to the variables. How can I do this?
Here is the section of code in my main file (T_n, T_l and period are previously set)
% Get data
lab_Data = readtable(file_lab, 'FileType', 'text');
lab_Data.Properties.VariableNames = {'Time', 'Flow', 'P_diff', 'Vol_ex'};
nalm_Data = readtable(file_nalm,'Sheet', sheet_name{4});
nalm_Data.Properties.VariableNames = {'Time', 'P_diff', 'Flow', 'Vol_tid'};
%Run tests
test1 = Tester(nalm_Data, lab_Data, T_n, T_l, period);
fileTrimmer(test1);
setFile(test1);
The construction of test1 is successful using a simple constructor. However, the other methods don't run and the error message appears to be directed at my attempt to access variables according to their names.
Here is a sample of my "fileTrimmer" code
function fileTrimmer(obj)
...
%trim nalm to first peak
f_nalm = obj.nalm_Data.Flow;
t_nalm = obj.nalm_Data.Time;
[~, loks] = findpeaks(f_nalm, t_nalm, 'MinPeakDistance', obj.period/2);
p1 = loks(1); % seconds of first peak.
Here is the constructor
classdef Tester
properties
sam_rate = .05
%Errors for FlowLab
erFLflow = .05 %(sl/min)
erFLvol = .01 %sl
erFLdp = .1 %mbar
erFLfreq = 1 %bpm
erFLcomp = 1 %ml/mbar
newFile
nalm_Data
lab_Data
T_n
T_l
period
end
methods
function obj = Tester(nalm_Data, lab_Data, T_n, T_l, period)
obj.nalm_Data = nalm_Data;
obj.lab_Data = lab_Data;
obj.T_n = T_n;
obj.T_l = T_l;
obj.period = period;
fprintf("Tester worked\n");
end
Edit: I actually managed to get around this issue by accessing table data by column indice rather than by their names, so as such have bypassed the error. I definately am still interested to know why it didn't work initially as well as whether it's best to use a value or handle class.
6 Comments
Walter Roberson
on 27 Aug 2019
Are you using a value class or a handle class?
Walter Roberson
on 27 Aug 2019
Please show the constructor for Tester
Steven Lord
on 27 Aug 2019
In addition to showing the constructor for Tester, show the full text of the error message you receive when you run your fileTrimmer function or method. Include all the text displayed in red (and if you receive any warning text in orange, include all of that too.)
Matt J
on 29 Aug 2019
Do you have a subsref method defined for the Tester class?
Corinne Gard
on 29 Aug 2019
Matt J
on 29 Aug 2019
No, but if you had defined a subsref method, it could explain any unexpected indexing behavior.
Accepted Answer
More Answers (0)
Categories
Find more on Matrices and Arrays 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!