textfile conversion ERROR after second loop: ??? Undefined function or method 'fieldnames' for input arguments of type 'cell'.

7 views (last 30 days)
Hi,
I have a question about a script that I wrote to make a conversion between 2 data log systems. I calculate the amount of files in my directory folder that I use. after I can convert the first but when the script wants to convert the second file ( the 2 files are completely the same.) I always get errors.
this is the error ??? Undefined function or method 'fieldnames' for input arguments of type 'cell'. this is my script. Is there anybody that can help me pleas?
close all
clear all
clc
% Work directory
cd('C:\Users\Toon\Documents\MATLAB\Conversion');
%Read the names of files in the work directory
fileName = dir('*.txt');
%Count amount of files
fileList=length(fileName);
for k = 1:fileList
% show wich file is converting
disp('Doing the following file in the loop')
disp('Number')
k
disp('Name')
fileName(k).name
% Import the file
newData = importdata(fileName(k).name,'\t' ,1);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData);
for i = 1:length(vars)
assignin('base', vars{i}, newData.(vars{i}));
end
%%Remove NaN at end of data
data(find(isnan(data(:,1)),1):end,:) = [];
%Extract channel names from header, remove units&spaces
headers = (textdata(1,3:end));
[headers temp] = strtok(headers,[' [']);
for i=1:length(headers);
try %Try because some ECU headers are not imported
eval([char(headers(i)) ' = data(:,i);']);
end
end
%%%Creating Laptrigger for DARAB
Laptrig = zeros(size(Time));
Laptrig(find(diff(Time)<-50)) = 1;
B_lappin = ones(size(Time));
B_lappin(find(diff(Time)<-50)) = 0;
lapctr = cumsum(Laptrig,1);
%%%%%%%%Modifications for WINDARAB%%%%%%%%%%%%%%%%%
laptime = Time;
speed = Speed_1;
xdist = cumsum(speed/3.6*mean(diff(Time)));
xtime = 0:median(diff(Time)):length(Time)*median(diff(Time))+10 ;
xtime=xtime(1:length(xdist));
%%%Conversion Throttle
ath = ECU_THROTTLE;
%%%Conversion brakes
pbrake_f = Brake_F;
pbrake_r = Brake_R;
brake_balance = Brake_Balance;
%%%Conversion other sensors
if exist('Longitudinal_a')
accx = Longitudinal_a;
clearvars Longitudinal_a;
else if exist('Acc_Long')
accx = Acc_Long;
clearvars acc_Long
else accx = 0;
end
end
if exist('Lateral_acc')
accy = Lateral_acc;
clearvars Lateral_acc;
else if exist('Acc_Lat')
accy = Acc_Lat;
clearvars Acc_Lat:
else accy = 0;
end
end
if exist('LR');
trvdam_rl = LR;
else trvdam_rl = 0 ;
end
if exist('RR')
trvdam_rr = RR;
else trvdam_rr = 0;
end
steer = Steer_Angle;
vwheel_fl = Speed_FL;
vwheel_fr = Speed_FR;
if exist('Speed_LR')
vwheel_rl = Speed_LR;
else vwheel_rl =0;
end
if exist('Speed_RR')
vwheel_rr = Speed_RR;
else vwheel_rr =0;
end
%%%Conversion engine
nmot = RPM;
ub = Battery;
tmot = ECU_WATER_T;
toil = ECU_OIL_T;
poil = ECU_OIL_P;
% ECU_FUEL_PMP
if exist('Pfuel')
pfuel = Pfuel;
else pfuel = 0;
end
%%%Conversion shift
gear = ECU_GEAR;
ugearp = ECU_GEAR_POT;
ags_upaddle_down = ECU_PAD_DWN;
ags_upaddle_up = ECU_PAD_UP;
ags_shift_state = ECU_SHAFT;
newChannels = {'xtime','xdist','speed','ath','pbrake_f','pbrake_r','brake_balance','Combined_G','accx','accy','trvdam_rl','trvdam_rr','steer','vwheel_fl','vwheel_fr','vwheel_rl','vwheel_rr','nmot','ub','tmot','toil','poil','pfuel','gear','ugearp','ags_upaddle_down','ags_upaddle_up','ags_shift_state','Laptrig','B_lappin','lapctr','laptime'};
clearvars Battery Brake_Balance Brake_F Brake_R CLUTCH Corner_Radius Damper_Velocity_FL Damper_Velocity_FR Damper_Velocity_RL Damper_Velocity_RR Datalogger_Tem Distance ECU_ALARM ECU_ALM_STAT ECU_BATTERY ECU_BUTTON ECU_CAM_REAL ECU_COIL_IJ ECU_COIL_IJ2 ECU_DAQ_GCC ECU_DIAG_PED ECU_FUEL_PMP ECU_GBOX_TMP ECU_GEAR ECU_GEAR_POT ECU_IGN_LIM ECU_INJ_LIM ECU_IN_STAT ECU_OIL_P ECU_OIL_T ECU_OUT_STAT ECU_PAD_DWN ECU_PAD_STATE ECU_PAD_UP ECU_PPS1_V ECU_PPS2_V ECU_PSD_T ECU_RAIN_LGH ECU_SHAFT ECU_STARTER1 ECU_STARTER2 ECU_SYC_LOST ECU_SYC_STAT ECU_THROTTLE ECU_TPS1_V ECU_TPS2_V ECU_VALVES ECU_VBAT_AUX ECU_V_BATT ECU_WATER_T GPS_Altitude GPS_Gyro GPS_Heading GPS_LatAcc GPS_Latitude GPS_LonAcc GPS_Longitude GPS_Nsat GPS_PosAccuracy GPS_Slope GPS_Speed KS_angle LR Load_Transefer_Lat Load_Transfer P_ENGINE Pfuel RPM RR Roll_F Roll_R Speed_1 Speed_2 Speed_FL Speed_FR Speed_LR Speed_RR Steer_Angle Steering_Theoretic Steering_Zero T_ENGINE Time USteer Vertical_acc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:length(newChannels)
A(:,j) = eval(char(newChannels(j)));
end
%%modify A to ensure the correct dimensions
A = A(1:length(A(:,(length(newChannels)))),1:length(newChannels));
cd('C:\Users\Toon\Documents\MATLAB\Conversion\OUTPUT');
fid = fopen(fileName(k).name,'wt');
for i=1:length(newChannels)
fprintf(fid,'%s' ,[char([newChannels(i)]) ' ' ]);
end
fprintf(fid,'%s \n',' ');
for i=1:length(A(:,1))
fprintf(fid,'%s \n',num2str([A(i,:)]));
end
fclose(fid);
clearvars xtime xdist speed ath pbrake_f pbrake_r brake_balance Combined_G accx accy trvdam_rl trvdam_rr steer vwheel_fl vwheel_fr vwheel_rl vwheel_rr nmot ub tmot toil poil pfuel gear ugearp ags_upaddle_down ags_upaddle_up ags_shift_state Laptrig B_lappin lapctr laptime;
cd('C:\Users\Toon\Documents\MATLAB\Conversion\OUTPUT');
end
kind regards

Answers (1)

Jan
Jan on 13 Jun 2015
There are assignin and eval commands in the code. They migth ovewrite function names by variables or other ugly things.
exist(name) checks for files, folders, java classes, variables and functions anywhere in the Matlab path. Therefore it is impossible to predict what happens. Use exist(name, 'var') instead.
The code can be simplified, e.g.:
for i=1:length(newChannels)
fprintf(fid,'%s' ,[char([newChannels(i)]) ' ' ]);
end
==>
fprintf(fid, '%s ', newChannels{:});
  1 Comment
Toon Mertens
Toon Mertens on 14 Jun 2015
I didn't change anything and now I get this error:
??? Undefined function or method 'fieldnames' for input arguments of type 'double'.
Error in ==> Conversion4best at 28 vars = fieldnames(newData);

Sign in to comment.

Categories

Find more on Data Import and Analysis 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!