Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 从文件导入变量_Matlab - Fatal编程技术网

Matlab 从文件导入变量

Matlab 从文件导入变量,matlab,Matlab,大家好,我有一个名为config.m的文件,其中包含一个变量列表和一些注释。我希望基本上通过另一个matlab脚本加载该脚本,以便识别和使用变量,并且可以轻松地进行更改。下面是我的带有变量的文件的外观 %~~~~~~~~~~~~~~~~~ %~~~[General]~~~~~ %~~~~~~~~~~~~~~~~~ %path to samtools executable samtools_path = '/home/pubseq/BioSw/samtools/0.1.8/samtools'; %

大家好,我有一个名为config.m的文件,其中包含一个变量列表和一些注释。我希望基本上通过另一个matlab脚本加载该脚本,以便识别和使用变量,并且可以轻松地进行更改。下面是我的带有变量的文件的外观

%~~~~~~~~~~~~~~~~~
%~~~[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';
我试过这个:

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.
load()不适用于包含文本的文件(即使是以matlab注释的形式)


您应该使用textscan()或dlmread(),向他们指定要跳过两行标题,或者要将“%”视为表示注释。

只需在另一个脚本中运行脚本config.m即可

config
请记住
config.m
文件应位于工作目录或MATLAB路径中

但是,我建议您从这个脚本创建一个函数,并返回一个包含所有参数作为字段的结构。然后,您将在主脚本中更加灵活,因为您可以为该结构指定任何名称

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

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

或者,您可以在
config.m
文件中定义一个具有常量属性的类:

classdef config

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

end
因此,您可以在另一个脚本中访问类属性:

config.samtools_path
config.output_path
要对其进行取整,可以将
config.m
文件放入包(+文件夹)中,并在脚本中显式导入它。假设您的包名为“foo”,并且Matlab路径上有“+foo”文件夹,那么您的脚本如下所示:

import foo.config

foo.config.samtools_path
foo.config.output_path