如何使.MAT文件的数组,并用其追加为 [英] How to make an array of .mat files and append it with a for

查看:327
本文介绍了如何使.MAT文件的数组,并用其追加为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code,

h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)'); %angry
h2 = dir('C:\Users\John\Documents\MATLAB\code for yannis\neutral(N)');%neutral
h3 = dir('C:\Users\John\Documents\MATLAB\code for yannis\happiness(F)');%happy

%fprintf('%s', filename);%filename h(i,1).name
fprintf('%d \n',numel(h1));
fprintf('%d \n',numel(h2));
fprintf('%d \n',numel(h3));

%fprintf('%d', max(numel(h1),numel(h2),numel(h3)));

A= [numel(h1) numel(h2) numel(h3)];

fprintf('%d \n \n \n', max(A));
fprintf('%s \n', h1(2).name);
%load('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\*.mat');
fprintf('%d \n \n \n', length(h1));
resultsdir = 'results';
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
array = [h1(2)];
for i=3:max(A)+3
    %s= 'C:\Users\John\Documents\MATLAB\code for yannis\anger(W)';
    thisfile = h1(i).name;
    destfile = fullfile(resultsdir, thisfile);
   thisdata = load(thisfile);

   %array[thisdata];

   %strcmp(h(i,1).name(1:2), s);
    %cat(1, array, h1(i,1).M);
    fprintf('%s \n', h1(i,1).name);
end

和我想保持在阵列中的所有文件.MAT为了与其他垫子的文件进行比较
在此先感谢

and i want to keep all the .mat files in an array in order to compare it with other mat files Thanks in advance

推荐答案

您可以选择顶层文件夹,并利用系统的 DIR 来电递归搜索所有文件符合您的标准( *垫,在这种情况下)

You can select the top level folder and utilize your system's dir call to recursively search for all files that match your criteria (*.mat, in this case)

例如:

mypath = uigetdir('', 'Select Top Level Folder');
oldpath = cd(mypath);  % cd to data directory for simpler dir call
[~, cmdout] = system('dir /S /B *.mat');
cd(oldpath);  % Return to previous path
mymatfiles = regexp(cmdout, '(.:\\[\w\-\\. ]+\.\w+)', 'match');

系统 调用返回一个长字符串的所有绝对路径到你的 *垫文件。我利用 正则表达式 拆分此为一个单元阵列,其中每个细胞就是一个单独 *垫文件的绝对路径。

The system call returns one long string with all of the absolute paths to your *.mat files. I utilize a regexp to split this into a cell array, where each cell is an absolute path to a single *.mat file.

请注意,这仅适用于Windows,因为它使用了MS-DOS DIR 命令。该功能可以很容易地适应其他操作系统,我只是不知道他们。

Note that this is Windows only because it uses the MS-DOS dir command. This function can easily be adapted to other operating systems, I just don't know them.

这篇关于如何使.MAT文件的数组,并用其追加为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆