Adding data from csv file into separate columns

조회 수: 11 (최근 30일)
Ariel  Edesess
Ariel Edesess 2016년 9월 29일
댓글: Ariel Edesess 2016년 9월 29일
Hi, I am trying to import data from several .csv files. I am successfully importing them but then I want to create a new array using data from the csv files. For instance, all csv files are data taken at the same location for different simulations. I would like to combine them so that my final data file has column 1: locations (which are the same for each), column 2: data from simulation 1, column 3: data from simulation 2, and so on...
So far, I have
name = 'lineX1_';
dataSaved = 'p_rgh_p_magU_';
which = 'mesh';
currentFolder = pwd;
dir = '/home/ariel/OpenFOAM/ariel-2.4.0/run/meshConvergenceJacobsen/postProcessing/'
disp(['Current Folder: ' currentFolder])
list = strtrim(ls(dir))
%mm = {'1';'2';'3';'4'};
mm = {'1';'4';};
data = [];
for jj = 1:length(mm)
data_current = load([dir name dataSaved which mm{jj} '.csv']);
x = data_current(:,1);
magU = data_current(:,6);
data = [x,magU];
end
This of course is rewriting the array each time with the new data rather than add the data to the next column. How would I do this?
Thanks in advance!
  댓글 수: 1
Jan
Jan 2016년 9월 29일
Do not use "dir" and "which" as names of variables, because this shadows builtin functions. The dir command is more direct than parsing the string output of ls.

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

답변 (1개)

Jan
Jan 2016년 9월 29일
dataC = cell(1, length(mm));
for jj = 1:length(mm)
data_current = load([dir name dataSaved which mm{jj} '.csv']);
dataC{jj} = data_current(:,[1, 6]);
end
data = cat(2, data_current{:});
  댓글 수: 1
Ariel Edesess
Ariel Edesess 2016년 9월 29일
Thanks very much, this is almost doing what I wanted but I'm getting the error:
Cell contents reference from a non-cell array object.
Error in meshConvergenceJacobsen (line 22)
data = cat(2,data_current{:});
It is reading in the data and making two separate arrays of two columns, but then not putting them together..

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by