Printing to a csv

조회 수: 18 (최근 30일)
Karthik Sharma
Karthik Sharma 2018년 5월 7일
댓글: Ameer Hamza 2018년 5월 8일
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개)

Ameer Hamza
Ameer Hamza 2018년 5월 7일
Save it using csvwrite()
csvwrite(firstfilename, {workOrder, sequenceNumber, delayDay});
csvwrite(secondfilename, {OperationID, cumulativeDelayDay});
  댓글 수: 3
Image Analyst
Image Analyst 2018년 5월 8일
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 2018년 5월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by