using fopen inside of a class

3 次查看(过去 30 天)
Hello there,
I am a graduate student looking to make a gcode writing script for our machine. I figured the easiest way would be to create a class with readable methods for me and my partner to share. We have previously been using fopen() to open a text file and write the gcode using fprintf statements, and I was hoping to translate that into a class; But I'm having problems with assigning the file to the object property.
Here's the class definition:
classdef Welder
properties
gcodefile
txtout = fopen(gcodefile,'w');
end
methods
function startUp(obj,Amperage)
obj.txtout = fopen(obj.gcodefile,'w');
fprintf(obj.txtout,"G0 G90 G54 G17 G40 G49 G80\nG21\nM3 S%.0f\nG4 P1.0\n",Amperage);
end
function moveX(obj,X)
fprintf(obj.txtout,'G1 X%.2f\n',X);
end
end
And here's the code in which I create an object of the welder class:
ArcNid = ARCNID;
% Startup
ArcNid.gcodefile = "GCodeOut.txt";
ArcNid.startUp(row,S);
ArcNid.turnArcOn(S,PF);
However, running this code yields the error message -
Error using U_Notch_SQR_Maker
Invalid default value for property 'txtout' in class 'ARCNID':
Unrecognized function or variable 'gcodefile'.
I've tried moving the definition of the txtout property around, but when changing the value inside of another method (I.E, the startUp method), the class property doesn't change, and any subsequent methods yield a similar 'unknown definition' error. Alternatively, I'm unsure if txtout should even be a property; I'm unused to matlab classes/etc., but I thought fopen returned essentially an interger file identifier, which sounds like it should then be a property.
Thanks in advance for the help!!
  2 个评论
Walter Roberson
Walter Roberson 2024-3-3
Your class does not initialize the property gcodefile
Your class tries to assign txtout the result of fopen() . But you also assign the same thing in startUp.
Sam Vellequette
Sam Vellequette 2024-3-4
Hi Walter,
About the double declaration - I'd like to be able to change the file if necessary, so in this slightly improved version I have gcodefile defined to be a string, and would like to be able to change Arcnid.gcodefile, run Arcnid.startUp, and have .startUp update the property txtout. Yet, when I run this, txtout does not change - it remains the same value.
Below is the slightly changed code:
classdef ARCNID
properties
gcodefile {mustBeText} = 'GCodeOut.txt'
txtout
end
methods
function startUp(obj,row,Amperage)
obj.txtout = fopen(obj.gcodefile,'w');
fprintf(obj.txtout,"(Code Produced from Excel Line: %.0f)\n",row+1);
fprintf(obj.txtout,"G0 G90 G54 G17 G40 G49 G80\nG21\nM3 S%.0f\nG4 P1.0\n",Amperage);
end
end
end
txtout (I believe) is a public property by default, so any idea why it won't budge?

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2024-3-4
txtout (I believe) is a public property by default, so any idea why it won't budge?
Your class is a value class. Value classes and handle classes behave differently in MATLAB. In particular, "If you pass this variable to a function to modify it, the function must return the modified object as an output argument."
So as you've written it, your startUp function modifies the copy of the object that was created for the method to operate on. When your method exits, because you're not returning that copy, the copy gets discarded along with the rest of the method workspace.
The easiest solution, if you want your class to be a value class, is to return the copy as an output argument.
See this documentation page for some discussion of the pros and cons of making your class a value class or a handle class.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by