How to create an automatic SKU generating code without too many loops?

조회 수: 5 (최근 30일)
Marc Santos
Marc Santos 2017년 7월 13일
답변: Karan Singh 2025년 2월 20일
I have a task of creating a database, and for every new item that is entered in the database, the code should be able to tell which names are already being used and which aren't. For instance, if I have a stored item that is colored silver it will be called SLV1 and if another new item is also silver, it will then be called SLV2. This logic applies to all the fields of the item code. The full item code would look something like this:
Item Code: SLV1-AEEGLB
  • Color - SLV1
  • Supplier - A
  • Brand - E
  • Length - E
  • Width - G
  • Batch # - L
  • Batch Date - B
Note: I just need an idea of how to approach it. Thank you!
This automatic SKU generator will be inserted in the lines with the comment signs as shown in the code below:
while current_quantity<quantity
W=numel(wholefoilrolls(:,1));
T=numel(cutfoilrolls(:,1));
for i=1:W
if strcmp(parentfoilcode_s,wholefoilrolls.ParentFoilCode(i,:))==1
x(i)=mod(wholefoilrolls.WholeFoilWidth(i),cutsize);
end
end
for j=1:T
if strcmp(parentfoilcode_s,cutfoilrolls.ParentFoilCode(j,:))==1
y(j)=mod(cutfoilrolls.CutFoilWidth(j),cutsize);
end
end
for i=1:W
for j=1:T
if min(x)<min(y) & min(x)>=target_value
if x(i)==min(x)
withdraw_whole=wholefoilrolls.WholeFoilCode(i,:);
wholefoilrolls(i,:)=[];
if current_quantity+floor(wholefoilrolls.WholeFoilWidth(i)/cutsize)<=quantity
% deposit_whole=table(
current_quantity=current_quantity+floor(wholefoilrolls.WholeFoilWidth(i)/cutsize);
else
% deposit_whole=table(
current_quantity=quantity;
end
elseif min(y)<min(x) & min(y)>=target_value
withdraw_cut=cutfoilrolls.CutFoilCode(j,:);
cutfoilrolls(i,:)=[];
if current_quantity+floor(cutfoilrolls.CutFoilWidth(j)/cutsize)<=quantity
% deposit_cut=table(
current_quantity=current_quantity+floor(cutfoilrolls.CutFoilWidth(j)/cutsize);
else
% deposit_cut=table(
current_quantity=quantity;
end
else
fprintf('There is no more foil with a parent foil code of %s that can satisfy the cut size requirement of %d', parentfoilcode_s, cutsize)
break
end
end
break
end
break
end
end

답변 (1개)

Karan Singh
Karan Singh 2025년 2월 20일
Have you tried lookup (or “dictionary”) structure. In MATLAB you might use a "containers.Map" so that when a new item is added you simply look up the current count for its code, increment it, and then build the SKU string. https://in.mathworks.com/help/matlab/ref/containers.map.html For example:
% Initialize (once) a map for color codes:
if ~exist('colorCountMap','var')
colorCountMap = containers.Map();
end
% Assume colorCode is determined (e.g., 'SLV' for silver)
if isKey(colorCountMap, colorCode)
newCount = colorCountMap(colorCode) + 1;
else
newCount = 1;
end
colorCountMap(colorCode) = newCount;
% Generate the color part of the SKU (e.g., SLV1, SLV2, etc.)
colorSKU = sprintf('%s%d', colorCode, newCount);
% Then, do similar things for the other fields (Supplier, Brand, etc.)
% and finally concatenate the pieces:
fullSKU = sprintf('%s-%s%s%s%s%s%s', ...
colorSKU, supplierCode, brandCode, lengthCode, widthCode, batchNum, batchDateCode);
Karan

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by