Printing to a csv

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

回答(1 个)

Save it using csvwrite()
csvwrite(firstfilename, {workOrder, sequenceNumber, delayDay});
csvwrite(secondfilename, {OperationID, cumulativeDelayDay});

3 个评论

Karthik Sharma
Karthik Sharma 2018-5-7
编辑:Karthik Sharma 2018-5-7
I got this error when I tried doing this, is this because workorder is a cell array?
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.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by