Accessing table variables in a class method
이전 댓글 표시
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
Walter Roberson
2019년 8월 27일
Are you using a value class or a handle class?
Walter Roberson
2019년 8월 27일
Please show the constructor for Tester
Steven Lord
2019년 8월 27일
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
2019년 8월 29일
Do you have a subsref method defined for the Tester class?
Corinne Gard
2019년 8월 29일
Matt J
2019년 8월 29일
No, but if you had defined a subsref method, it could explain any unexpected indexing behavior.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!