フィルターのクリア

How can i append my csv file during looping?

2 ビュー (過去 30 日間)
NG
NG 2014 年 8 月 15 日
コメント済み: Michael Haderlein 2014 年 8 月 15 日
This is what I currently have, but each loop will over-write the csv file, so i cannot append the csv file, is there anyu way to fix it ? -------------------------------------------------FileNames=dir('*.csv'); for i=1:length(FileNames) [num,txt,all] = xlsread(FileNames(i).name); fid = fopen(FileNames(i).name); hdrs = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s',1,'delimiter',','); data = textscan(fid,'%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f',length(xlsread(FileNames(i).name))+1,'delimiter',','); fclose(fid); outCell = cell(size(data{1},1),length(hdrs)); for j=1:length(hdrs) if isnumeric(data{j}) outCell(:,j) = num2cell(data{j}); else outCell(:,j) = data{j}; end end z=outCell(1:length(xlsread(FileNames(i).name)),11); y= outCell(1:length(xlsread(FileNames(i).name)),1); x=outCell(1:length(xlsread(FileNames(i).name)),2); c=cell2mat(z);

回答 (1 件)

Michael Haderlein
Michael Haderlein 2014 年 8 月 15 日
I guess you don't want to append something to the csv file but to append something at your z,y,x variables, right? Also, I suppose that before the last line (c=...) there's a "end" keyword missing.
Then you need to use indexing. z,y,x built up from one-dimensional column arrays, so you can write
z(:,i)=outCell(...);
One comment on your z,y,x lines: Now you always read each file 4 (!) times, one time to get the data and another 3 times to just get the length. You can do that more efficiently by using the size of your data.
  2 件のコメント
NG
NG 2014 年 8 月 15 日
FileNames=dir('*.csv'); for i=1:length(FileNames) [num,txt,all] = xlsread(FileNames(i).name); fid = fopen(FileNames(i).name); hdrs = textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s',1,'delimiter',','); data = textscan(fid,'%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f',length(xlsread(FileNames(i).name))+1,'delimiter',','); fclose(fid); outCell = cell(size(data{1},1),length(hdrs)); for j=1:length(hdrs) if isnumeric(data{j}) outCell(:,j) = num2cell(data{j}); else outCell(:,j) = data{j}; end end z(:,i)=outCell(1:length(xlsread(FileNames(i).name)),11); y(:,i)= outCell(1:length(xlsread(FileNames(i).name)),1); x(:,i)=outCell(1:length(xlsread(FileNames(i).name)),2); c=cell2mat(z); end ----------------------------------- Is the script above right????However, the matlab shows that Subscripted assignment dimension mismatch. Thanks
Michael Haderlein
Michael Haderlein 2014 年 8 月 15 日
However, I don't get an error. Maybe your different files have different lengths? I mean, different number of lines? Then, you cannot put them together to one matrix.
Also, I believe that you don't want to have the c=cell2mat(z) inside the for loop. It will be overwritten in every iteration. I suppose it should be outside after the loop. And still your code is very slow as you read every file 5 times. You should work on that.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by