【问题标题】:MATLAB loading data from multiple .mat filesMATLAB 从多个 .mat 文件加载数据
【发布时间】:2015-04-22 08:36:40
【问题描述】:

我的数据是多个文件中的 x,y 坐标

a=dir('*.mat')
b={a(:).name}

将文件名加载到元胞数组中

如何使用循环将每个文件中的一列数据顺序加载到新/单独数组的连续行中......?

我一直在单独使用例如

Load(example1.mat)
A(:,1)=AB(:,1)
Load(example2.mat)
A(:,2)=AB(:,1)
Load(example3.mat)
A(:,3)=AB(:,1)

显然非常原始且耗时!!

我的 Matlab 技能很弱,所以非常感谢任何建议

干杯

再次非常感谢,我还在研究如何阅读代码,但我是这样使用的; a=dir('*.mat'); b={a(:).name}; 测试1=零(数字(b),1765); 对于 k=1:numel(b) S=load(b{k}); 然后我使用以下代码创建了一个 PCA 聚类图 test1(k,:)=S.AB(:,2); end [wcoeff,score,latent,tsquared,explained] = pca(test1,... 'VariableWeights','variance'); c3 = wcoeff(:,1:3) coefforth = inv(diag(std(test1)))*wcoeff; I = c3'*c3 cscores = zscore(test1)*coefforth; figure() plot(score(:,1),score(:,2),'+') xlabel('1st Principal Component') ylabel('2nd Principal Component') -

我正在使用“gname”来标记聚类图上的点,但发现该点只是简单地从 1 标记到数组中的行数.....我本来想问你这个问题,但我如果我使用 'gname(b)' 这只是通过反复试验发现的,这会用 b 中列出的 .names 标记点.....

但是,一旦我标记了很多点,clusterplot 开始看起来非常繁忙/混乱,所以现在我想知道是否可以通过拖动圆形或选择几个点将文件名提取到列表中,我认为这是可能的正如我已经阅读了一些相关主题......但是任何关于 gname 或从集群图中标记/提取标签的提示/建议将不胜感激。再次为我的格式道歉,我还在习惯这个网站!!!

【问题讨论】:

  • 嗨,斯蒂芬。我看到您提议对我的回答进行编辑;但它很可能会被系统拒绝为“尝试回复”。您可以改为编辑原始问题吗?谢谢!
  • 我好像删掉了一些原来的问题!!老实说,真的在这个网站上苦苦挣扎!
  • 哈哈没问题!今天晚些时候我会看一下并编辑你的问题。谢谢!

标签: matlab


【解决方案1】:

这是一种方法。希望我得到了你想要的正确:)

代码已注释,但如果有不清楚的地方,请提出任何问题。

a=dir('*.mat');
b={a(:).name};

%// Initialize the output array. Here SomeNumber depends on the size of your data in AB.
A = zeros(numel(b),SomeNumber);

%// Loop through each 'example.mat' file
for k = 1:numel(b)

%// ===========  
%// Here you could do either of the following:

1)
   %// Create a name to load with sprintf. It does not require a or b.
   NameToLoad = sprintf('example%i.mat',k);

   %// Load the data
   S = load(NameToLoad);


2) 
   %// Load directly from b:
   S = load(b{k});
   %// ===========

%// Now S is a structure containing every variable from the exampleX.mat file. 
%// You can access the data using dot notation.

 %// Store the data into rows of A
   A(k,:) = S.AB(:,1);

end

希望这就是你的意思!

【讨论】:

  • 非常感谢,我还在研究如何阅读代码,但我是这样使用的; a=dir('*.mat'); b={a(:).name};测试1=零(数字(b),1765);对于 k=1:numel(b) S=load(b{k}); test1(k,:)=S.AB(:,2); end [wcoeff,score,latent,tsquared,explained] = pca(test1,... 'VariableWeights','variance'); c3 = wcoeff(:,1:3) coefforth = inv(diag(std(test1)))*wcoeff; I = c3'*c3 cscores = zscore(test1)*coefforth; figure() plot(score(:,1),score(:,2),'+') xlabel('1st Principal Component') ylabel('2nd Principal Component')
  • 我正在使用 gname 来显示集群图标签,但不知道如何将其链接到 'a' 或 'b' 中的实际文件名 抱歉这个问题的格式!!
  • 抱歉,我才看到这条评论!您可以在原始问题中添加信息(通过编辑)吗?谢谢!
猜你喜欢
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2021-07-04
  • 2015-06-13
  • 2011-08-25
  • 2014-08-28
  • 2012-09-30
相关资源
最近更新 更多