【问题标题】:matlab reading variables with varying lengths into the workspacematlab将不同长度的变量读入工作区
【发布时间】:2013-06-04 08:51:02
【问题描述】:

这是一个后续问题

Reading parameters from a text file into the workspace

我想知道,我将如何阅读以下内容:

% ---------------------- details --------------------------
%---------------------------------------------------------------
% location:
   lat = 54.35
   lon = -2.9833
%

Eitan T 建议使用:

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

从文件中获取信息然后

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

获取相关参数。我怎么能改变这个来阅读以下内容:

% ---------------------- details --------------------------
%---------------------------------------------------------------
% location:
   lat = 54.35
   lon = -2.9833
   heights = 0, 2, 4, 7, 10, 13, 16, 19, 22, 25, 30, 35
   Volume = 6197333, 5630000, 4958800, 4419400, 3880000, 3340600,   
        3146800, 2780200, 2413600, 2177000, 1696000, 811000
%

变量应该包含等号后面的所有数据点(直到下一个变量的开始,在这种情况下为 Volume)?

感谢您的帮助

【问题讨论】:

  • Volume 的数据实际上是在多行上,还是您在这个问题中只是这样写的?
  • 我会使用textscan()regexp() 读取整个文件,然后使用str2double() 进行转换。我没有给出任何例子,因为从我之前的回答中,我没有得到关于你的文件是否表现良好的反馈,现在这是一个后续问题,可能还有更多问题,直到你展示关于你的文件的全部交易拥有和你需要的东西。

标签: matlab fopen


【解决方案1】:

这是一种方法,它使用一些肮脏的字符串黑客和eval 来获得结果。这适用于您的示例,但我不会真正推荐它:

fid = fopen('data.txt');
contents = textscan(fid, '%s', 'CommentStyle', '%', 'Delimiter', '\n');
contents = contents{1};

for ii = 1:length(contents)
  line = contents{ii};
  eval( [strrep(line, '=', '=['), '];'] ) # convert to valid Matlab syntax
end

更好的方法是使用textscan 读取每一行

for ii = 1:length(contents)
  idx = strfind(contents{ii}, ' = ');
  vars{ii} = contents{ii}(1:idx-1);
  vals(ii) = textscan(contents{ii}(idx+3:end), '%f', 'Delimiter', ',');
end

现在变量varsvals 具有变量的名称和它们的值。要提取值,您可以执行类似的操作

ix = strmatch('lat', vars, 'exact');
lat = vals{ix};

ix = strmatch('lon', vars, 'exact');
lon = vals{ix};

ix = strmatch('heights', vars, 'exact');
heights = vals{ix};

ix = strmatch('Volume', vars, 'exact');
volume = vals{ix};

【讨论】:

    【解决方案2】:

    这可以通过两步法完成:

    1. 读取前导字符串(第一个元素)、等号(忽略),并将该行的其余部分作为字符串(第二个元素)
    2. 将这些字符串的其余行转换为浮点数(第二个元素)

    然而,这里有一个小缺点;您的台词似乎遵循两种格式;一个是步骤 1) 中描述的那个,另一个是前一行的延续,其中包含数字。

    因此,需要一个额外的步骤:

    1. 读取前导字符串(第一个元素)、等号(忽略),并将该行的其余部分作为字符串(第二个元素)
    2. 遇到“其他格式”时会失败。检测到这一点,纠正这一点,然后继续
    3. 将这些字符串的其余行转换为浮点数(第二个元素)

    我认为这样可以解决问题:

    fid = fopen('data.txt');
    
    C = [];
    try
        while ~feof(fid)
    
            % Read next set of data, assuming the "first format"
            C_new = textscan(fid, '%[^= ]%*[= ]%s', 'CommentStyle', '%', 'Delimiter', '');
            C     = [C; C_new]; %#ok
    
            % When we have non-trivial data, check for the "second format"
            if ~all(cellfun('isempty', C_new))
    
                % Second format detected!
                if ~feof(fid)
    
                    % get the line manually
                    D = fgetl(fid);
                    % Insert new data from fgetl
                    C{2}(end) = {[C{2}{end} C{1}{end} D]};
                    % Correct the cell
                    C{1}(end) = [];
    
                end
    
            else
                % empty means we've reached the end
                C = C(1:end-1,:);
    
            end
    
        end
        fclose(fid);
    
    catch ME
        % Just to make sure to not have any file handles lingering about
        fclose(fid);
        throw(ME);
    
    end
    
    % convert second elements to floats
    C{2} = cellfun(@str2num, C{2}, 'UniformOutput', false);
    

    【讨论】:

      【解决方案3】:

      如果你可以摆脱多行卷线,你写的是有效的matlab。因此,只需使用 run 命令将参数文件作为 matlab 脚本调用即可。

      run(scriptName)
      

      正如其他人所展示的那样,您唯一的其他选择是编写最终看起来像一个混蛋的 Matlab 解析器。绝对有比这样做更好的消磨时间的方法!

      【讨论】:

        猜你喜欢
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-21
        相关资源
        最近更新 更多