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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by