matlab - 如何从不同的 .mat 文件加载表和保存表并为每个文件创建新的表维度
问题描述
我正在使用 Matlab 2019 并保存了大量具有相同名称的不同表,每个表都保存在具有不同名称的 .mat 文件中。我想加载它们并将它们一起保存在一个新表中。
我需要一个具有相同变量(variable1、variable2、variable3)和数字的表,但对于不同的 .mat 文件有一个额外的维度。即在下面的虚拟代码中,表 allData 的大小应该是:2 x 3 x 2。
如果我像这样简单地连接它们 [Results1.Inpts; Results2.Inpts],那么表格是 4 x 3,我无法区分来自不同文件的数据。
我也有大量的 .mat 文件,所以我想像这里一样自动化它(https://www.mathworks.com/matlabcentral/answers/233745-loading-mat-files-with-same-variable -name ),我尝试为下面的表格执行此操作,但没有成功。
请在下面找到我的虚拟代码。
%% Create data and .mat files
clc;
clear;
close all;
format long;
Names = {'variable1','variable2','variable3'};
Numbers1 = [2, 3, 4; 3, 4, 5];
Numbers2 = [1, 4, 10; 2, 6, 3];
Inpts = array2table(Numbers1,'VariableNames',Names);
save('Results1.mat');
clear Inpts;
Inpts = array2table(Numbers2,'VariableNames',Names);
save('Results2.mat');
clear;
%% Analyse data
files = dir('*.mat');
allData = table(size(files));
for ii=1:length(files)
aFileData = load(files(ii).name);
allData{ii} = aFileData.Inpts;
end
解决方案
我认为您只需要在连接之前在每个表中添加一列
allData = cell(numel(files),1); % Store the tables in a cell
for ii=1:length(files)
aFileData = load(files(ii).name);
% Add a column
aFileData.FileName(:) = {files(ii).name};
% Stick the modified table in the cell
allData{ii} = aFileData.Inpts;
end
% Merge tables
allData = vertcat( allData{:} );
推荐阅读
- angular - Angular 7 TestBed.overrideProvider 不适用于 ActivateRoute
- android - 如何解决 Kotlin 类型转换问题?
- mysql - mySQL中创建过程的两个IN参数
- apache-kafka - 两个(Kafka)S3 连接器不能同时工作
- html - shopify中响应式设计的CSS问题
- ios - 使用 iOS(Objective-C)SDK 的自定义 Branch.io 事件未显示在仪表板中?
- cloudflare - 使用 Cloudflare 通配符和来源为 docker 容器配置 Traefik
- xquery - 我在将 EBNF 语法转换为 Antlr4 语法时遇到问题
- php - PHP 更新 MS SQL 数据库
- php - 交易中的 Laravel 特征