Printing to a csv

18 views (last 30 days)
Karthik Sharma
Karthik Sharma on 7 May 2018
Commented: Ameer Hamza on 8 May 2018
I'm doing some data interpretation and I need to print some of my output to a csv file. I need to print the workOrder, Sequence Number, delay days to one csv and OperationID and cumulative delay days to another csv. How exactly could I do that?
[workOrder,sequenceNumber,operationID,startDate,finishDate,runHours] = importfile('details.csv', 2, inf);
workID(1) = workOrder(1);
operationArray(1) = operationID(1);
j = 1;
k = 1;
operationArray = sort(operationID);
operationList(1) = operationArray(1);
for i=1: 1 : 3980
if strcmp(workOrder(i),workOrder(i+1)) == false
j = j +1;
workID(j) = workOrder(i+1);
end
end
for i = 1 : 3980
if strcmp(operationArray(i),operationArray(i+1)) == false
k = k+1;
if k > 30
for z = 1:size(operationList)
if strcmp(operationList(z),operationArray(i))
break
end
end
break
end
operationList(k) = operationArray(i+1);
end
end
j = 1;
delayDay = zeros(size(workOrder));
for i=2: 1 : 3979
if strcmp(workOrder(i),workOrder(i+1)) && strcmp(workOrder(i),workOrder(i-1))
t1Num = datenum(startDate(i+1));
t2Num = datenum(finishDate(i));
delayDay(j) = t1Num - t2Num;
end
if strcmp(workOrder(i),workOrder(i - 1)) == false && strcmp(workOrder(i),workOrder(i + 1)) == false
t1 = datetime(2017,5,1,4,0,0);
t1Num = datenum(t1);
t2Num = datenum(startDate(i));
delayDay(j) = t2Num - t1Num;
end
if strcmp(workOrder(i),workOrder(i+1)) == false
j= j+1;
end
end
operationTable = [operationID, num2cell(datenum(startDate)), num2cell(datenum(finishDate))];
operationTableSorted = sortrows(operationTable);
j = 1;
for i = 1:30
operationIDcalc(i) = operationList(i);
end
cumulativeDelayDay = zeros(size(operationIDcalc));
j = 1;
for i =2 : 3980
if strcmp(operationTableSorted(i,1),operationTableSorted(i+1,1)) && strcmp(operationTableSorted(i,1),operationTableSorted(i-1,1))
t1Num = datenum(startDate(i+1));
t2Num = datenum(finishDate(i));
cumulativeDelayDay(j) = cumulativeDelayDay(j) + t1Num - t2Num;
end
if (strcmp(operationTableSorted(i,1),operationTableSorted(i - 1,1)) == false && strcmp(operationTableSorted(i,1),operationTableSorted(i + 1,1)) == false) || (strcmp(operationTableSorted(i,1),operationTableSorted(i+1,1)))
t1 = datetime(2017,5,1,4,0,0);
t1Num = datenum(t1);
t2Num = datenum(startDate(i));
cumulativeDelayDay(j) = cumulativeDelayDay(j) + t2Num - t1Num;
end
if strcmp(operationTableSorted(i,1),operationTableSorted(i+1,1)) == false
j= j+1;
end
end

Answers (1)

Ameer Hamza
Ameer Hamza on 7 May 2018
Save it using csvwrite()
csvwrite(firstfilename, {workOrder, sequenceNumber, delayDay});
csvwrite(secondfilename, {OperationID, cumulativeDelayDay});
  3 Comments
Image Analyst
Image Analyst on 8 May 2018
If workOrder, sequenceNumber, and delayDay are column vectors, then use brackets instead of braces so that you're writing a regular matrix instead of a cell array.
Ameer Hamza
Ameer Hamza on 8 May 2018
For this method to work all the variables should have the same number of elements. Also, they should all be column vector as mentioned by @Image Analyst. First, make sure that they have the same element and then try
csvwrite(firstfilename, {workOrder(:), sequenceNumber(:), delayDay(:)});
this will convert them to column vector before writing to the csv file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!