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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Walter Roberson
Walter Roberson am 13 Mär. 2022
Can you attach the first few lines of the csv for testing?
Benjamin Pommer
Benjamin Pommer am 14 Mär. 2022
Thank you. It already worked out. One variable was without a label which led to problems in the code.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by