Problem with table column not being double when workspace identifies it as such

4 views (last 30 days)
Hi, I import a data table textfile (usually there are several and it is looped) and have had some issues with it, I can overcome them but when I try to filter some data in the data column I am after (named 'trajectory_deg' in the original file but I separate it into a separate variable called traj) it will not allow it, telling me
"Undefined operator '>' for input arguments of type 'table'.
Error in untitled3 (line 73)
traj_copy((80>traj_copy)&(traj_copy>100))=NaN;"
In my workspace tray is class double, when I whos trap it tells me it is double, yet this problem persists, any ideas?
close all
clear all
clc
%%Get files
Folder_used = dir('/Users/files4data/*.txt');
files = Folder_used;
amountFILES=numel(files);
for k=1:amountFILES
% for k=1:1
opts = detectImportOptions(files(k).name);
data=readtable(files(k).name,opts);
possibleTrajStr={'Trajectory_deg','trajectory','trajectory_deg','Trajectory','Traj_deg','traj'};
possibleNozStr={'ExpID','Exp_ID','Exp ID'};
headers_in_data =data.Properties.VariableNames;
disp *************************************************************
%%*************************************************************
file=(files(k).name);
left_color = [0 0 0];
right_color = [0 0 0];
set(figure,'defaultAxesColorOrder',[left_color; right_color],...
'units','normalized','outerposition',[0 0 1 1]);
%%*************************************************************
ColForm=varfun(@class,data,'OutputFormat','cell');
[ColIndexLog,ColIndexDouble]=ismember(ColForm,'cell') ;
CellsInTable=find(ColIndexDouble);
indices = find(abs(CellsInTable)>4);
CellsInTable(indices) = [];
for g=1:numel(CellsInTable)
B=CellsInTable(g);
C=(headers_in_data(B));
CCC=char(string(C{:}));
XC=data.(CCC);
dataLoop=XC;
logicalIndex = ismember(dataLoop, 'UND. -60001');
dataLoop(logicalIndex) = {'NaN'};
S = sprintf('%s*', dataLoop{:});
N = sscanf(S, '%f*');
N2=array2table(N);
data.(CCC)=(N2);
end
TrajOuty = ismember(headers_in_data,possibleTrajStr);
[~,TrajColID] = find(TrajOuty);
traj=data(:,TrajColID);
traj=table2array(traj);
whos traj
traj_copy=traj;
traj_copy((80>traj_copy)&(traj_copy>100))=NaN;
traj_copy=(deg2rad(traj_copy-90))*1000;
% traj((80>traj)&(traj>100))=NaN;
% traj=(deg2rad(traj-90))*1000;
end

Accepted Answer

Madeline Gardner
Madeline Gardner on 3 Jul 2018
Hello!
So there are a few things going on here.
One thing I noticed is that you turn N, the trajectory array, into a table before putting it back into data. This means that the table ends up nested, where the column inside the table is full of tables. If instead you just put N into the table directly, then you don't have to deal with that.
Second, if you index into a table using smooth parenthesis (), then it returns a table, which is also why your logical indexing wasn't working. If instead you use dot indexing and smooth parentheses, you can retrieve the actual data in the column by index, which can be indexed into logically.
Here is the code that seems to work for me -- I've included comments where I changed things. I hope it does what you intended!
Folder_used = dir('/Users/files4data/*.txt');
files = Folder_used;
amountFILES=numel(files);
for k=1:amountFILES
% for k=1:1
opts = detectImportOptions(files(k).name);
data=readtable(files(k).name,opts);
possibleTrajStr={'Trajectory_deg','trajectory','trajectory_deg','Trajectory','Traj_deg','traj'};
possibleNozStr={'ExpID','Exp_ID','Exp ID'};
headers_in_data =data.Properties.VariableNames;
disp *************************************************************
%%*************************************************************
file=(files(k).name);
left_color = [0 0 0];
right_color = [0 0 0];
set(figure,'defaultAxesColorOrder',[left_color; right_color],...
'units','normalized','outerposition',[0 0 1 1]);
%%*************************************************************
ColForm=varfun(@class,data,'OutputFormat','cell')
[ColIndexLog,ColIndexDouble]=ismember(ColForm,'cell');
CellsInTable=find(ColIndexDouble);
indices = find(abs(CellsInTable)>4);
CellsInTable(indices) = [];
for g=1:numel(CellsInTable)
B=CellsInTable(g);
C=(headers_in_data(B));
CCC=char(string(C{:}));
XC=data.(CCC);
dataLoop=XC;
logicalIndex = ismember(dataLoop, 'UND. -60001');
dataLoop(logicalIndex) = {'NaN'};
S = sprintf('%s*', dataLoop{:});
N = sscanf(S, '%f*');
% N2=array2table(N) - Deleted
data.(CCC)=N;
end
TrajOuty = ismember(headers_in_data,possibleTrajStr);
[~,TrajColID] = find(TrajOuty);
traj=data.(TrajColID); % Changed to dot indexing
% traj=table2array(traj) - Deleted
whos traj
traj_copy = traj;
traj_copy((80>traj_copy)|(traj_copy>100)) = NaN; % Changed & to |
traj_copy=(deg2rad(traj_copy-90))*1000
end
  3 Comments
Stephen Devlin
Stephen Devlin on 4 Jul 2018
As much as I appreciate the solution I appreciate the explanation, thank you Madeline, I couldn't figure out how 99% of the time the code this was derived from had no issues, now I can see it. Many thanks Steve

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!