How to automatically import multiple column vectors into Matlab from multiple .csv files using Live Script and a For Loop?
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Manuel Rodrigo Lomas Anza
le 7 Fév 2022
Commenté : Highphi
le 9 Fév 2022
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
le 8 Fév 2022
Can you post sample CSV files and more information about what kind of output you want?
Réponse acceptée
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
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 :)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!