How to properly write a matrix in a for loop?

조회 수: 3 (최근 30일)
David Mrozek
David Mrozek 2021년 3월 16일
댓글: David Mrozek 2021년 3월 19일
Hello everyone,
I have been trying various ways to create a gcode for 3D printers. My probably last goal will be to create such a code only in MATLAB without external software being active. Hence I have written a code which should give me a total of 12 gcode unnested gcode instructions (equal to the data_test attachment of a 1 x 12 cell):
for gc = 1:12
append_1 = transpose(repelem("G1",height(Importset{1, gc})));
append_2 = transpose(repelem("F1200",height(Importset{1, gc})));
append_3 = transpose(repelem("X",height(Importset{1, gc})));
append_4 = transpose(repelem("Y",height(Importset{1, gc})));
append_5 = transpose(repelem("Z",height(Importset{1, gc})));
T3 = Importset{1, gc}(:,1);
T4 = Importset{1, gc}(:,2);
T5 = Importset{1, gc}(:,3);
g_code = [append_1 + append_3 + T3 + append_4 + T4 + append_5 + T5 + append_2]; % this gives me only one out of the inital 12 cells !!
end
The result, see the attachment file "g_code_unfinished", is just one out of the twelve cells and not the desired full twelve cells. So I would like to ask you where my mistake is?
  댓글 수: 1
David Mrozek
David Mrozek 2021년 3월 16일
I want to achieve the same data structure (like in the g_code_unfinished attachment) for all my cells. Currently I only have one out of the 12 cells in my desired structure (because my for loop is wrong)

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

채택된 답변

Aghamarsh Varanasi
Aghamarsh Varanasi 2021년 3월 19일
편집: Aghamarsh Varanasi 2021년 3월 19일
Hi David,
For the 'gcode' variable to save all the data for 12 cells of input, 'gcode' also needs to be a cell array of size 12. And the calculated value needs to be stored in the variable 'gcode'. Small tweaks to your code can help you achieve this.
% initialize g_code to be a cell array of size 1 x 12
g_code = cell(1,12);
for gc = 1:12
... % your code for calculating other variables goes here
% append the current result of this iteration of for loop to the g_code variable
g_code{gc} = [append_1 + append_3 + T3 + append_4 + T4 + append_5 + T5 + append_2];
end
You can now save the variable 'gcode' to a mat file. For more information go through the documentation pages of for loop and cell arrays.
I would also recommend you to take up the 'free' MATLAB OnRamp training. This can help you understand the basic MATLAB concepts.
  댓글 수: 1
David Mrozek
David Mrozek 2021년 3월 19일
Ok the first line of yout code did the job.
I have tried framing the gc variable in the {} before, but it did not worked because of the missing reference. Thanks!!!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by