How to automatically import multiple column vectors into Matlab from multiple .csv files using Live Script and a For Loop?

11 vues (au cours des 30 derniers jours)
Hello, I'm a coding and generally computing aliterate. I've used Matlab to evaluate data for my experiments in the University and now I'm also using it at work. Normally I get a large set of .csv files which I can individually import into Matlab. Today I found a tool "Import Data" and I've been experimenting with it, but I can't find a way to "add" into the Live Script a "For Loop code" so that the Files will automatically be read one by one storing my data into column vectors (arrays) with the same name but different numbers. Example: Pol_X_W, Pol_X_I, where X can be 000, 005, 010, 015... and so on. And also reading it from the same files by those arbitraty X numbers. I have no clue how a computer works, so I really would need you to explain to me either how the computer itself processes that information, so that I can find extra information on that on the web and then figure out a coding method or maybe theres a generic solution I haven't tried out. More specifically I just need the computer to understand that X is a variable that has to run throu a list of numbers I want to choose within the "Live Script" of the Data Import Tool.
Here is what the Code looks like:
Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["Pol_20_W", "Pol_20_I"];
opts.VariableTypes = ["double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
tbl = readtable("/Users/User/Downl/Po/Spectrum_pol_020.csv", opts);
Convert to output type
Pol_20_W = tbl.Pol_20_W;
Pol_20_I = tbl.Pol_20_I;
Clear temporary variables
clear opts tbl
The 20 could be 020 I don't mind, but I would like to put 'X' and make a list of all the posible X values and automatically go throu them... cuz they are like 80 files... Thank you so much for reading this.
  2 commentaires
Benjamin Thompson
Benjamin Thompson le 8 Fév 2022
Can you post sample CSV files and more information about what kind of output you want?
Manuel Rodrigo Lomas Anza
Manuel Rodrigo Lomas Anza le 9 Fév 2022
The CSV files are like two colomns each with a title and then a set of 1340 rows. so they are like two 1341x2 matrices. And the Output I was looking for was separate column vectors. Like 1340x1 colomuns. But if you know how to manage these files in 1340x80 choosing arbitrarily which column goes to what matrix/tbl... that would be dope too.

Connectez-vous pour commenter.

Réponse acceptée

Highphi
Highphi le 9 Fév 2022
here ya go:
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
% opts.VariableNames = ["Pol_20_W", "Pol_20_I"]; leave this out for now
opts.VariableTypes = ["double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
for i = 1:80 % or whatever number, just follow the logic below
myNumber = num2str(i); % convert number to string
if length(myNumber) == 1
myNumber = ['0', myNumber]; % if less than 10 -> if you go over 100 this needs to be tweaked
end
varName1 = ['Pol_', myNumber, '_W'];
varName2 = ['Pol_', myNumber, '_I'];
opts.VariableNames = [varName1, varName2]; % NOW we use it
csvFilename = ['/Users/User/Downl/Po/Spectrum_pol_0', myNumber, '.csv'];
tbl = readtable(csvFilename, opts);
eval(['values_W = tbl.', varName1, ';'];
eval(['values_I = tbl.', varName2, ';'];
% ^ do all of that inside the for loop, going through all files
% then do something with values_W & values_I
% check out the table2array function for more user friendly matrices
end
clear opts
  2 commentaires
Manuel Rodrigo Lomas Anza
Manuel Rodrigo Lomas Anza le 9 Fév 2022
Hi! I called the function "Respuesta.m".
I thought it would work, but I tried to run it and it said this.
"Error using Respuesta (line 26)
Unrecognized table variable name 'Pol_0_W'."
The program is creating a table 1340x1 instead of 1340x2, with each a column name. I guess that's the problem, but idk why. And in the table the column is named "Pol_0_WPol_0_I".
Besides that there are two ")". Thank you so much for the help. If you got time and know a fix for this I would appriciate it if you could give me a hint.
Highphi
Highphi le 9 Fév 2022
of course!
hm, Pol_#_W it should never be 0 because we're starting at i = 0
but I also see that the evals forgot the ')' at the end (easy fix)
do you think you could give me a sample file of the data so I can actually import it? that would help me fully solve this :)

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by