Adding data from csv file into separate columns

12 ビュー (過去 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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by