You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: generate_from_template.m
    path of file: ./repos/toolkit-legacy/utilities
    
    the code of the file until where you have to start completion: function generate_from_template(filename, template, varargin)
% generate_from_template Generate a new file from a template file
%
% Generate a new file from a template file by % inserting given variables 
% into the placeholders. The placeholders in the template file are denoted
% by ''{{ variable_name }}''.
%
% Input:
% - filename (string): Path to the destination file.
% - template (string): Path to the template file.
% - varargin (cell): Pairs of strings, the first string denotes the name of
% the variable and the second one denotes the value.

    
function generate_from_template(filename, template, varargin)
% generate_from_template Generate a new file from a template file
%
% Generate a new file from a template file by % inserting given variables 
% into the placeholders. The placeholders in the template file are denoted
% by ''{{ variable_name }}''.
%
% Input:
% - filename (string): Path to the destination file.
% - template (string): Path to the template file.
% - varargin (cell): Pairs of strings, the first string denotes the name of
% the variable and the second one denotes the value.
%


report = fileread(template);

if (ischar(filename))
    fid = fopen(filename, 'w');
    close_file = 1;
else
    fid = filename;
    close_file = 0;
end