Hello,
I have a .txt file with 7lines and 4 columns, and 4 csv files(with names a.csv, b.csv, c.csv and d.csv). In the first column there are the names of my .csv files( Imean a.csv, b.csv,c.csv and d.csv).
I would like via a loop to read each of my .csv files , depending on the line with the same name/line( Imean in the first column of the first Line of my .txt file has the name 'a.csv'. Depending on this I would to read the 'a.csv' file.
Could anyone help me?

2 commentaires

Rik
Rik le 28 Mai 2020
So each txt file contains the file name of the csv file you want to read?
And what have you tried so far to read either the txt or csv files?
Ivan Mich
Ivan Mich le 28 Mai 2020
I am importing the .txt file and the 4 .csv files in order to understand what I want to make/
To Be honest I have no idea whih command should I use , and that's why I am asking there...

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 28 Mai 2020

0 votes

How to read text files is maybe the most frequently asked question. Among many solutions, you can either directly read the txt file with native Matlab functions, or use my readfile function. In this case the added benefit of that file is 0, but in the future it may be handy to already have that function.
Then you can read the relevant csv file with the readmatrix function.

4 commentaires

Ivan Mich
Ivan Mich le 28 Mai 2020
I know these commands, thank you. But I would like to read from the .txt files each one of .csv files via a loop.
Do you know how?
Rik
Rik le 28 Mai 2020
  1. read the text file to a cell array with the readfile function
  2. loop through the lines to parse the file name and use that as the input for the readmatrix function
Show what you have tried. What is the exact issue you are having? This isn't your first question. You can even re-use the code that was provided to you in previous questions.
Ivan Mich
Ivan Mich le 28 Mai 2020
Modifié(e) : Ivan Mich le 28 Mai 2020
I used
clc
clear
t4=readtable('final.txt','readvariablenames', true);
name=t4{:,1}
filename1= 'a.csv';
[d1,tex]= xlsread(filename1);
The problem is that I do not know what to do in order to continue my code and make what I want
Why did you ignore my advice about using readfile? Also, at least skip that readvariablenames, it doesn't do what you need it to. Anyway, the code below contains the required edits to fix your code.
t4=readtable('final.txt');
name=t4{:,1};
data=cell(size(name));
for n=1:numel(name)
csv_file_name=name{n};
%read the data to a cell array
data{n}=readmatrix(csv_file_name);
end
%instead of using data1, data2, and data3, you can use data{1}, data{2}, and data{3}

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by