首页 文章

MATLAB从多个.mat文件加载数据

提问于
浏览
0

我的数据是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技能很弱,所以任何建议都会感激不尽

干杯

非常感谢,我还在想弄清楚如何阅读代码,但我这样使用它;一个= DIR( '垫'); B = {A(:)名称}; TEST1 =零(numel(b)所示,1765);对于k = 1:numel(b)S = load(b );然后我使用以下代码创建PCA集群图test1(k,:) = S.AB(:,2);结束[wcoeff,得分,潜伏,tsquared,解释] = 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 .....

然而,一旦我标记了很多点,群集图就会显得非常繁忙/凌乱,所以现在我想知道可以通过拖动或选择几个点来将文件名提取到列表中,我认为这是可能的,因为我有阅读一些相关的主题.....但任何关于gname或标签/提取标签的提示/建议将非常感谢 . 再次为我的格式道歉我仍然习惯了这个网站!

1 回答

  • 0

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

    代码已注释,但如果不清楚,请询问任何问题 .

    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
    

    希望这就是你的意思!

相关问题