【问题标题】:read the contents of the structure of a matlab file读取matlab文件结构的内容
【发布时间】:2023-01-12 22:05:15
【问题描述】:

我有几个 matlab 文件,每个文件都有一个包含(在工作区中)表(我感兴趣的)和其他变量的结构,我想检索这些表中包含的数据并将它们放入数据框中(使用连接只有一个数据框)。

你能帮我么?

我想找到解决我的问题的方法,或者至少要探索一些途径,以便更好地理解我必须做什么

【问题讨论】:

    标签: python matlab file recovery


    【解决方案1】:

    您可以使用循环遍历每个 MATLAB 文件,将其内容加载到工作区,并提取表变量。然后,您可以使用 MATLAB 函数“vertcat”(用于垂直串联表)或“horzcat”(用于水平串联表)将表串联成一个数据帧。以下是如何完成此操作的示例:

    % Create an empty dataframe to store the concatenated tables
    df = table();
    
    % List of file names
    file_list = {'file1.mat', 'file2.mat', 'file3.mat'};
    
    % Loop through each file
    for i = 1:length(file_list)
        % Load the contents of the file into the workspace
        load(file_list{i});
        % Extract the table variable(s) from the workspace
        tbl =  eval(table_name);
        % Concatenate the table(s) to the dataframe
        df = vertcat(df, tbl);
    end
    

    您可以将 table_name 替换为 matlab 文件中的表变量的名称。 在连接它们之前,您可能还想检查每个文件中的表名是否相同。 另外,如果你想水平连接表格,你可以使用“horzcat”函数而不是“vertcat”。

    df = [df tbl];
    

    同样重要的是要注意,此示例假设您感兴趣的表变量在加载文件后位于工作区中,否则您将不得不使用函数“who”来检查 mat 中的变量文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2013-02-07
      • 2018-09-19
      • 2011-12-12
      • 2011-04-21
      相关资源
      最近更新 更多