Error using horzcat Dimensions of matrices being concatenated are not consistent.

4 visualizzazioni (ultimi 30 giorni)
I'm trying to do some basic data manipulation and I'm trying to create a matrix with some of my data. The error that I'm running into is when I try to concatenate my vectors together I get a Dimensions are not consistent error even though I am positive they are. The error is happening in the last line of my code. I'm also including pictures of the variables in question from my workspace.
<<
<<-n-lt--lt--matlabcentral-answers-uploaded_files-116478-Screenshot-20-234-.png>>
>>
>>
[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(workID)); for i=2: 1 : 3980
if strcmp(workOrder(i),workOrder(i+1)) && strcmp(workOrder(i),workOrder(i-1))
t1Num = datenum(startDate(i));
t2Num = datenum(finishDate(i));
delayDay(j) = delayDay(j) + t2Num - t1Num;
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) = delayDay(j) + t2Num - t1Num;
end
if strcmp(workOrder(i),workOrder(i+1)) == false
j= j+1;
end
end
operationTable = [workOrder,datenum(startDate),datenum(finishDate)];
end
Any help would be appreciated thanks!
  3 Commenti
David Fletcher
David Fletcher il 7 Mag 2018
Even though they are the same size, the fundamental problem is you are trying to concatenate a cell array with a normal array. A trivial solution would be:
operationTable={workOrder,datenum(startDate),datenum(finishDate)}
It can become somewhat tiresome to retrieve information from such a structure though

Accedi per commentare.

Risposta accettata

Ameer Hamza
Ameer Hamza il 7 Mag 2018
Modificato: Ameer Hamza il 7 Mag 2018
As you shown in the comments that workOrder is a cell array. You cannot concatenate vectors of different classes in one matrix. All elements of the matrix must belong to same class. However, in your case, you can convert them to the cell arrays before concatenation
operationTable = [workOrder, num2cell(datenum(startDate)), num2cell(datenum(finishDate))];
The advantage is that you can access them in the same way as a matrix, you only need to use curly brackets. For example operationTable{100,:} will retrieve all values from 100th row.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by