Ideal way to import csv data and create column vectors that will be variables i want to work with
Ältere Kommentare anzeigen
Hello,
I have a .csv file that i wish to import and create column vecotrs (variables) that i can manipulate further with my code. Below is my code. I also wish to remove NaN values from all columns that have them. In additon i want to muliply all of the EMG data columns by 1000 which is why that number is there towards the end of my code. This code seems to work but i was told to not use eval in code writing and just wanted to see if someone can check it and offer any other suggestions to code it differently.
Thank you in advance!
filename='RESP_Trial_3.csv';
column_names={'RLT','RLTx','RLTy','RLTz','RUT','RUTx','RUTy','RUTz','RIC','RICx','RICy','RICz',...
'RPEC','RPECx','RPECy','RPECz','RPS','RPSx','RPSy','RPSz','RD','RDx','RDy','RDz',...
'RRA','RRAx','RRAy','RRAz','ROB','ROBx','ROBy','ROBz','Analog21','Analog22','Analog23','Sync'};
EMGVariables={'RLT','RUT','RIC','RPEC','RPS','RD','RRA','ROB','Sync'};
%read the imported matrix table and create data struct to store variables
%Create variables in the workspace with the same names as column names and
%assign the corresponding columns of the matrix as their values
matrix = readmatrix(filename,'HeaderLines', 7);
data = struct();
for i = 1:numel(column_names)
variable_name = column_names{i};
assignin('base',variable_name, matrix(:,i));
end
%Remove NaN
for i = 1:numel(EMGVariables)
variable_name = EMGVariables{i};
variable = 1000*eval(variable_name);
variable(isnan(variable))=[];
assignin('base', variable_name, variable);
end
1 Kommentar
Stephen23
am 23 Jan. 2023
"This code seems to work but i was told to not use eval in code writing..."
EVAL and ASSIGNIN and various others:
"...and just wanted to see if someone can check it and offer any other suggestions to code it differently"
Use a table.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!