user1178682
user1178682

Reputation:

Import variables from file

Hi so I have a file called config.m that contains a list of variables along with some comments. I wanted to basically load that script up through another matlab script so that the variables would be recognized and used and could also be changed easily. Here is what my file with the variables looks like.

%~~~~~~~~~~~~~~~~~
%~~~[General]~~~~~
%~~~~~~~~~~~~~~~~~
%path to samtools executable
samtools_path = '/home/pubseq/BioSw/samtools/0.1.8/samtools';
%output_path should be to existing directory, script will then create tumour
%and normal folders and link the bam files inside respectively
output_path = '/projects/dmacmillanprj/testbams'; 
prefix = %prefix for output files
source_file = % from get_random_lines.pl, what is this?
% The window size
winSize = '200';
% Between 0 and 1, i.e. 0.7 for 70% tumour content
tumour_content = '1';
% Should be between 0 and 0.0001
gc_window = 0.005;
% Path to tumour bam file
sample_bam = '/projects/analysis/analysis5/HS2310/620GBAAXX_4/bwa/620GBAAXX_4_dupsFlagged.bam';
% Path to normal bam file
control_bam = '/projects/analysis/analysis5/HS2381/620GBAAXX_6/bwa/620GBAAXX_6_dupsFlagged.bam';

I have tried this:

load('configfile.m')
??? Error using ==> load
Number of columns on line 2 of ASCII file /home/you/CNV/branches/config_file/CopyNumber/configfile.m
must be the same as previous lines.

Upvotes: 1

Views: 4486

Answers (3)

mdh
mdh

Reputation: 5563

Alternatively, you could define a class with constant properties in your config.m file:

classdef config

    properties (Constant)
        samtools_path = '/home/pubseq/BioSw/samtools/0.1.8/samtools';
        output_path = '/projects/dmacmillanprj/testbams';
    end

end

Thereby, you can access the class properties in another script:

config.samtools_path
config.output_path

To round it up, you could place your config.m file into a package (+ folder) and import it explicitly in your script. Assuming your package would be called "foo" and the "+foo" folder on your Matlab path, your script would look as follows:

import foo.config

foo.config.samtools_path
foo.config.output_path

Upvotes: 1

yuk
yuk

Reputation: 19870

Just run the script config.m inside another script as

config

Remember config.m file should be in the working directory or in MATLAB path.

However I would recommend you to create a function from this script and return a structure with all the parameters as fields. Then you will be more flexible in your main script since you can assign any name to this structure.

function param = config()
param.samtools_path = '/home/pubseq/BioSw/samtools/0.1.8/samtools';
param.output_path = '/projects/dmacmillanprj/testbams';
% ... define other parameteres

In the main script:

P = config;
st_dir = P.samtools_path;
% ...etc...

Upvotes: 1

jzworkman
jzworkman

Reputation: 2703

load() is not suitable for files that contain text (even in the form of matlab comments.)

You should use textscan() or dlmread(), specifying to them that you want to skip two header lines or that you want to treat '%' as indicating a comment.

Upvotes: 0

Related Questions