157k views
1 vote
Write a function writel that will write the length of a side of a square. If only one argument is passed to the function, it is the length, and the output will go to the Command Window. If two arguments are passed, however, the first argument will be the length and the second argument will be the file identifier for an output file to which the length should be printed.

2 Answers

6 votes

Answer: Answer to this question for MatLab

Step-by-step explanation:

function out = write1(length,varargin)

if nargin == 1

disp(length)

end

if nargin == 2

fid = fopen(varargin{1},'w');

fprintf(fid,'%d',length) %

fclose(fid);

end

end

%varargin{1} needs to be the name of a file you already created

User John Park
by
3.7k points
4 votes

def writel(length, file_name = ""):

if file_name!="":

f = open(file_name, "w")

f.write(str(length))

f.close()

else:

print(str(length))

writel(3, "FileName.txt")

I hope this helps!

Remember, you must have the file created prior to running this program. Best of luck.

User Ry Biesemeyer
by
4.0k points