How to create a matrix from csv files?

조회 수: 4 (최근 30일)
Nicolas Sylvestre
Nicolas Sylvestre 2019년 11월 28일
답변: Andrei Bobrov 2019년 11월 29일
Hi,
I have a text matrix with my file names called log_files
log_files = ['file1.csv';'file2.csv';'file3.csv';'file1.csv';'file1.csv';'file4.csv']
I also have a matrix with the data of each csv files, called datafiles
datafiles = [1 1 1;2 2 2;3 3 3;4 4 4]
Where [1 1 1] is the data in file1.csv, [2 2 2] is the data in file2.csv, [3 3 3] is the data in file3.csv and [4 4 4] is the data in file4.csv
What I want, is a matrix called log_data that would look like this (according to log_files)
log_data= [1 1 1;2 2 2;3 3 3;1 1 1;1 1 1;4 4 4]
Thank you for the help you will provide.
  댓글 수: 1
Rik
Rik 2019년 11월 29일
Do you really have a char array? Or is it actually a cell array with the file names?
You can use a loop to read each file and store them in a matrix. What have you tried so far?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 11월 29일
filenames = {'file1.csv', 'file2.csv', 'file3.csv', 'file4.csv'};
log_files = {'file1.csv';'file2.csv';'file3.csv';'file1.csv';'file1.csv';'file4.csv'};
[~, idx] = ismember(log_files, filenames);
log_data = datafiles(idx,:);

Andrei Bobrov
Andrei Bobrov 2019년 11월 29일
[~,~,i] = unique(log_files);
log_data = datafiles(i,:);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by