Creating a loop to receive data from multiple files

1 view (last 30 days)
I have 400 csv files, which contains the location of points and its temperature value. What I want to do is to build a command that calculate derivative dT/dx which are equally spaced from each csv file, receive the specific row that has the maximum derivative, and save the information of every 400 files in a single file. I would have done it if there are only about 10files, but for 400 files must be done with loops. Please help me. Thank you
  1 Comment
young jin Kwak
young jin Kwak on 13 Nov 2016
I think I am 80% all the way there, but cannot get the txt file with data for 421 files. If I run the following code, txt file takes the output only for sec, and rewrite it over and final output is only for last step. Please let me know what I have done wrong
format long
numfiles =421;
mydata = cell(1,numfiles);
for k = 1:numfiles
myfilename = sprintf('side20m0%d.csv',k-1); mydata{k} = cell(1,numfiles);
x_location = importfile(myfilename);
temp = importfile1(myfilename);
deriv = zeros(502,1);
for j=1:502
if j<502
deriv(j) = (temp(j+1)-temp(j))/(x_location(j+1)-x_location(j));
else
deriv(j) = 0;
end
end
M = [deriv,x_location];
[minA loc] = min(M(:,1));
xAtMin = zeros(420,1);
xAtMin(k) = M(loc,2);
fileID = fopen('Result.txt','w');
fprintf(fileID,'%6.6f\n', xAtMin(k));
fclose(fileID); end

Sign in to comment.

Answers (1)

Shivaputra Narke
Shivaputra Narke on 13 Nov 2016
Hey there, I am not understanding what is exactly stopping you to do this ? I guess, you are not doing it because of time constraint on processing 400 files (even through loop xlsread/csvread will take more time that depends upon the file size too).
Temp=[]; Locn=[]; for i=1:length(DataFiles) [Data,text,~]=xlsread(DataFiles{i}); % For e.g. in excel, column 4 contains temperature and column 5 contains location of points Temp(:,i)=Data(:,4); Locn(:,i)=Data(:,5); end
  1 Comment
young jin Kwak
young jin Kwak on 13 Nov 2016
Thank you for your help. I am really newbie in MATLAB. I made a example here. I have 400 files of csv as below. Each file indicate different time steps.(1.csv ~ 400.csv) What I want to do is for each time step, calculate the derivative dT/dx, (dx = same , equally distributed), and find the row that has maximum dT/dx, and save that specific location value with max derivative for each time steps. So output would be a single txt or csv file with 400rows. I can import a single data and calculate the value, but do not have any idea to do it for all 400 files. Thank you.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!