Importing multiple data and assigning each columns into different variables

38 visualizzazioni (ultimi 30 giorni)
Hello, I am a student trying to learn Matlab for the first time.
I've never learned such programs before so I am really new to all this systems.
Now, I have many different mocap data mostly in .txt and .csv files.
They contain different sizes of matrix such as file#1 with 2341 x 31, file#2 with 2751 x 31
But they all have same numbers of raws with same variables which look like this:
Time, variable 1, variable 2, variable 3, variable 4,.... variable n
So I really want to import them easily into my workspace,
and then assign each raw into single variables, which I have been coding every single line until now like this:
data = readtable("01_Angle.csv"); % or txt.data = importdata("01_Angle.txt");
LEL = table2array(data(:,2)); % variable 1
REL = table2array(data(:,3)); % variable 2
LKN = table2array(data(:,4));
RKN = table2array(data(:,5));
LHP_AB = table2array(data(:,6));
LHP_FL = table2array(data(:,7));
RHP_AB = table2array(data(:,8));
RHP_FL = table2array(data(:,9));
LSH_AB = table2array(data(:,14));
LSH_FL = table2array(data(:,15));
RSH_AB = table2array(data(:,16));
RSH_FL = table2array(data(:,17));
But as files and variables are getting bigger, it is very time consuming, and I'm sure there's better way to do this.
I've been googling and searching on this community, and found many similar questions.
But they were bit different cases from this and some I couldn't understand.
I've used this sample here that Mathwork provides,
but then I got a 'cell' data which I couldn't find out how to assign them into variables in a for loop.
files=dir('*.txt');
numfiles = length(files);
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('file%d.txt', k);
mydata{k} = importdata(myfilename);
end
Please help me out with this! Thank you
  2 Commenti
Stephen23
Stephen23 il 22 Lug 2022
"But as files and variables are getting bigger, it is very time consuming, and I'm sure there's better way to do this."
Yes there is: the best way is to just use the original table: then you do not duplicate any data and you can simply and reliably refer to the columns/variables of the table using their header names.
"...I couldn't find out how to assign them into variables in a for loop."
That approach forces you into writing slow, complex, inefficient, obfuscated, insecure, buggy code that is hard to debug:
Note that your proposed approach is inherently fragile/buggy, e.g. consider when the header contains characters that are not valid in a variable name (whereas this causes no issues at all when using the original table, the recommended approach).
"I am a student trying to learn Matlab for the first time."
That is the right time to learn good code habits, and to learn from the mistakes of others.
SBJ
SBJ il 25 Lug 2022
Thank you for your kind answer and I really get your point.
However, since I've been struggling for days to accomplish this loop,
could you still show me the way to accomplish this?
I'll use the way you've recommended, but still I want to learn how to create the loop the way I was planning to do.
Thanks again though for your recommendations.

Accedi per commentare.

Risposta accettata

KSSV
KSSV il 22 Lug 2022
data = readtable("01_Angle.csv"); % or txt.data = importdata("01_Angle.txt");
data = table2array(data) ;
You have each variable into a column. You can access them by data(:,1), data(:,2) etc. No need to save each column into a different variable.
  1 Commento
SBJ
SBJ il 25 Lug 2022
Thank you for your answer.
But I intended to learn the process of importing multiple files and creating variables using loops.
Could you show me how to save them into different variables?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Workspace Variables and MAT Files in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by