MATLAB Answers

Accessing table variables in a class method

4 views (last 30 days)
Corinne Gard
Corinne Gard on 27 Aug 2019
Commented: Matt J on 29 Aug 2019
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

Show 3 older comments
Matt J
Matt J on 29 Aug 2019
Do you have a subsref method defined for the Tester class?
Corinne Gard
Corinne Gard on 29 Aug 2019
No. Do you think it would be best if I had one? To be honest I am new to oop in Matlab, I am much more familiar with Java.
Matt J
Matt J on 29 Aug 2019
No, but if you had defined a subsref method, it could explain any unexpected indexing behavior.

Sign in to comment.

Accepted Answer

Catalytic
Catalytic on 29 Aug 2019
Edited: Catalytic on 29 Aug 2019
Here is an example to demonstrate that accessing tables by variable name works fine with value classes. Subclassing from 'handle' will also give the same result.
classdef myclass
properties
T %table prooperty
end
methods
function obj=myclass(T)
obj.T=T;
end
function accessT(obj)
obj.T.Var2,
end
end
end
T =
5×3 table
Var1 Var2 Var3
____ ____ ____
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
>> obj=myclass(T)
>> obj.accessT
ans =
6
7
8
9
10

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.