将参数从文本文件读入工作区 [英] Reading parameters from a text file into the workspace

查看:94
本文介绍了将参数从文本文件读入工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含以下信息:

I have a file which has the following information:

% ---------------------- location details --------------------------
%
% lat : latitude  [minimum = -90, maximum = 90, unit =  
%       degrees north]
% lon : longitude [ minimum = -360, maximum = 360, unit = 
%       deg east]
% z: altitude (above sea level, m)
%---------------------------------------------------------------
% location:
   lat = 54.35
   lon = -2.9833

这是文件的一小部分.

我想将其中一些信息读入MATLAB中,然后可以在其中使用这些信息来执行一些计算.我想读入MATLAB的文件部分是文本文件中未注释的部分, ie 在行的开头有一个%,然后该变量应为保存在工作区中.例如,我想拥有:

I would like to read some of this information into MATLAB, where the information can then be used to perform some calculations. The part of the file that I would like to read into MATLAB are those in the text file that are not commented, i.e have a % at the start of the line, and the variable should then be saved in the workspace. For example, I would like to have:

lat = 54.35
lon = -2.9833

在工作区中.

我将如何处理?我已经读过关于textscanfopen的信息,尽管在这种情况下这些似乎并没有帮助我.

How would I go about this? I have read about textscan and fopen, although these don't really seem to help me in this instance.

推荐答案

快速而又肮脏的方法

我想到的最简单的解决方案确实是使用textscan :),并且由于这些行以有效的MATLAB语法方便地编写,因此以后可以使用eval对其进行评估.首先将每一行读为一个字符串(忽略标题中的注释)

The quick-and-dirty approach

The simplest solution I could think of to read this file indeed employs textscan :) and since the lines are conveniently written in valid MATLAB syntax, you could use eval later to evaluate them. Start by reading each line as one string (ignoring the comments in the header)

fid = fopen(filename);
C = textscan(fid, '%s', 'Delimiter', '', 'CommentStyle', '%')
fclose(fid);

然后一行一行地输入到eval中,以在MATLAB工作区中创建变量:

Then feed the lines one by one into eval to create the variables in the MATLAB workspace:

cellfun(@eval, C{1});

这是将行解释为MATLAB命令,即按文件中的名称创建变量 并分配适当的值.如果要抑制eval的输出,可以使用evalc代替吸收输出":

What this does is interpret the line as a MATLAB command, i.e create variables as named in the file and assign the appropriate values. If you want to suppress the output of eval, you can use evalc instead to "absorb the output":

cellfun(@evalc, C{1}, 'UniformOutput', false);

这应该对您的基本示例有效,但是如果您有多个参数实例,则它将失败.另外请注意,eval系列的速度非常慢.

This should work for your basic example, but it would fail if you have more than one instance of any parameter. Also note that the eval family is notoriously slow.

如果文件结构中的行具有parameter name = number模式,则可以更智能地读取行:

If the lines in your file structure have the parameter name = number pattern, you can read the lines more intelligently:

fid = fopen(filename);
C = textscan(fid, '%[^= ]%*[= ]%f', 'CommentStyle', '%')
fclose(fid);

模式中的%[^= ]匹配第一个字符,直到第一个空格或等号. %*[ =]忽略等号和任何尾随空格,然后将数值与%f匹配.生成的单元格数组C将参数名称存储在第一个单元格中,并将其相应的值存储在第二个单元格中.

The %[^= ] in the pattern matches the first characters until the first space or equality sign. The %*[ =] ignores the equality sign and any trailing spaces, and then the numerical value is matched with %f. The resulting cell array C stores the parameter names in the first cell and their corresponding values in the second cell.

现在,由您决定要处理已解析的数据.例如,要提取latlon的所有值,可以执行以下操作:

Now it's up to you to manipulate the parsed data. For instance, to extract all values of lat and lon, you can do this:

lat = C{2}(strcmp(C{1}, 'lat'));
lon = C{2}(strcmp(C{1}, 'lon'));

如果您有多个纬度"行,则lat将是一个包含所有这些值的数组.

If you have more than one "lat" line, lat will be an array holding all these values.

这篇关于将参数从文本文件读入工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆