Matlab functino with columns of a table of a .mat or .csv file as an input argument

조회 수: 1 (최근 30일)
I hava a the following question. I have a function which shall have as an argument either variables from a table from the workspace or directly the .mat or .csv file and extract the necessary data from it. The function arguments shall be used for further calculation of what so ever.
The first time, I tried to get access to the variables velocity through the csv file, but I get an error message:
[A,X,res,x_vec,x_pred] = DMD_Kopie('655_GMaps_Route_02182022_095722_Simulation_Run.csv',5,25,4)
Unable to resolve the name
GMapsRoute02182022095722SimulationRun.velocity.
Error in DMD_Kopie (line 7)
v =
GMapsRoute02182022095722SimulationRun.velocity(1:end)';
function [A,X,res,x_vec,x_pred] = DMD_Kopie(filename,n,N_pred,states)
data = readtable(filename);
v = data.velocity(1:end)';
a = data.acc(1:end)';
t = data.t(1:end)';
E = data.energy(1:end)';
%further code
end
The second time, I tried to get access to the variables velocity through the corresponding .mat file. In that case, I got a struct array and I get the following error:
[A,X,res,x_vec,x_pred] = DMD_Kopie('GMapsRoute02182022095722SimulationRun',5,25,4)
Unrecognized field name
"velocity".
Error in DMD_Kopie (line 6)
v = data.velocity(1:end)';
[A,X,res,x_vec,x_pred] = DMD_Kopie('GMapsRoute02182022095722SimulationRun.mat',5,25,4)
Unable to resolve the name
GMapsRoute02182022095722SimulationRun.velocity.
Error in DMD_Kopie (line 7)
v =
GMapsRoute02182022095722SimulationRun.velocity(1:end)';
function [A,X,res,x_vec,x_pred] = DMD_Kopie(filename,n,N_pred,states)
data = load(filename);
v = data.velocity(1:end)';
a = data.acc(1:end)';
t = data.t(1:end)';
E = data.energy(1:end)';
%further code
end
Now, I tried to add an index to get access to the first field of the produced struct array. An again I get the same error:
[A,X,res,x_vec,x_pred] = DMD_Kopie('GMapsRoute02182022095722SimulationRun',5,25,4)
Unrecognized function or variable
'GMapsRoute02182022095722SimulationRun'.
Error in DMD_Kopie (line 7)
v =
GMapsRoute02182022095722SimulationRun(1).velocity(1:end)';
function [A,X,res,x_vec,x_pred] = DMD_Kopie(filename,n,N_pred,states)
data = load(filename);
v = data(1).velocity(1:end)';
a = data(1).acc(1:end)';
t = data(1).t(1:end)';
E = data(1).energy(1:end)';
end
I also tried to delete the (1:end)' part of the lline but the same error occurs.
The last time, I loaded the variable into the workspace and tried to get access to them but again I get an error message:
[A,X,res,x_vec,x_pred] = DMD_Kopie(5,25,4)
Unable to resolve the name
GMapsRoute02182022095722SimulationRun.velocity.
Error in DMD_Kopie (line 7)
v =
GMapsRoute02182022095722SimulationRun.velocity(1:end)';
function [A,X,res,x_vec,x_pred] = DMD_Kopie(n,N_pred,states)
v = GMapsRoute02182022095722SimulationRun.velocity(1:end)';
a = GMapsRoute02182022095722SimulationRun.acc(1:end)';
t = GMapsRoute02182022095722SimulationRun.t(1:end)';
E = GMapsRoute02182022095722SimulationRun.energy(1:end)';
%further code
end
The fist line of the corresponding .csv (655_GMaps_Route_02182022_095722_Simulation_Run.csv) file looks like that:
t,d,velocity,velocity_kmh,speed,torque,gear,lat,lon,alt,slope,curvature,curvature_rad,bearing,spd_lim,energy,acc
0,0.0000000,0.0000000,0.0000000,0.0000000,800.0000000,0.0000000,1,48.1863178,16.3779272,245.4757642,0.0000000,0.0000000,0.0001000,153.8148865,50.0000000,0.0000000,0.0000000
The name of the converted .csv file is the following GMapsRoute02182022095722SimulationRun.mat .
Do you have any idea how to solve this?
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 3월 13일
Can you attach the first few lines of the csv for testing?
Benjamin Pommer
Benjamin Pommer 2022년 3월 14일
Thank you. It already worked out. One variable was without a label which led to problems in the code.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by