How to properly write a matrix in a for loop?

2 vues (au cours des 30 derniers jours)
David Mrozek
David Mrozek le 16 Mar 2021
Commenté : David Mrozek le 19 Mar 2021
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 commentaire
David Mrozek
David Mrozek le 16 Mar 2021
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)

Connectez-vous pour commenter.

Réponse acceptée

Aghamarsh Varanasi
Aghamarsh Varanasi le 19 Mar 2021
Modifié(e) : Aghamarsh Varanasi le 19 Mar 2021
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 commentaire
David Mrozek
David Mrozek le 19 Mar 2021
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!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by