Adding data from csv file into separate columns
11 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!