How to properly write a matrix in a for loop?

3 次查看(过去 30 天)
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