简体   繁体   English

如何在Octave / Matlab中将“ .mat”文件作为参数传递

[英]How to pass a “.mat” file as an argument in Octave/Matlab

I have a number of .mat files. 我有许多.mat文件。 I have written a short script where i use file1.mat and analyse the data. 我写了一个简短的脚本,在其中我使用file1.mat并分析了数据。 The next step in the process would be doing the same thing for file2.mat. 该过程的下一步将是对file2.mat做同样的事情。 One way to do this could be to simply copy my previous code and replace all the "file1.mat" with "file2.mat" and do the same for 3, 4.... However, I feel that there has to be a more elegant solution. 一种方法是简单地复制我以前的代码,并用“ file2.mat”替换所有的“ file1.mat”,然后对3,4进行同样的操作。但是,我觉得必须有一个更优雅的解决方案。 The optimal situation would be if i could write a function that takes the filename (preferably not the whole path) as an argument. 最佳情况是,如果我可以编写一个以文件名(最好不是整个路径)作为参数的函数。 Is this possible? 这可能吗?

I have scurried the net and the closest I get is the "feval" function which works fine if i have .m files but not at all with .mat files. 我已经搜寻了网络,而我得到的最接近的是“ feval”功能,如果我有.m文件,但对于.mat文件则根本不起作用。

Is there a way to pass .mat files to a matlab-function? 有没有一种方法可以将.mat文件传递给Matlab函数?

Let's say that you have the following script, which just loads some .mat file and processes two variables - 假设您有以下脚本,该脚本仅加载一些.mat文件并处理两个变量-

load('C:\data\input1.mat'); %// loads x, y into the workspace
z = x + y;
save('C:\data\output1.mat', 'z');

and you want to also process input2.mat , input3.mat etc. The best way is to write a function that wraps up all this work into a neat blob - 并且您还希望处理input2.matinput3.mat等。最好的方法是编写一个函数,将所有这些工作包装成一个整洁的blob-

function processData(fnameIn, fnameOut)
    pathIn = fullfile('C:\data', fnameIn);
    pathOut = fullfile('C:\data', fnameOut);
    load(pathIn); %// loads x, y into the workspace
    z = x + y;
    save(pathOut, 'z');
end

Now you can call it like this 现在您可以这样称呼它

processData('input1.mat', 'output1.mat')
processData('input2.mat', 'output2.mat')

etc, or even better 等,甚至更好

inputNames = {'input1.mat', 'input2.mat' };
outputNames = {'output1.mat', 'output2.mat'};

for i = 1:length(inputNames)
    processData(inputNames{i}, outputNames{i});
end

or, if your filenames happen to be structured, you can just do 或者,如果您的文件名恰好是结构化的,则可以

for i = 1:2
    infile = sprintf('input%d.mat', i);
    outfile = sprintf('output%d.mat', i);
    processData(infile, outfile);
end

Another possible solution would be to write your function so that it doesn't do any file loading or saving at all, but instead it receives some data as input and returns it as output. 另一种可能的解决方案是编写您的函数,使其根本不执行任何文件加载或保存操作,而是接收一些数据作为输入,并将其作为输出返回。 This is more flexible, because now you can control how you access your data (eg maybe you want to load it all into the workspace before processing any of it - you can do that now, whereas before the data loading, processing and saving were all tied together in one function). 这更加灵活,因为现在您可以控制访问数据的方式(例如,也许您希望在处理任何数据之前将其全部加载到工作区中-您现在就可以这样做,而在数据加载,处理和保存之前捆绑在一起)。 The processData function would look like this processData函数如下所示

function dataOut = processData(dataIn)
    x = dataIn.x;
    y = dataIn.y;
    dataOut.z = x + y;
end

and you use it like this if you want to load the files and then process them one at a time, 如果您要加载文件然后一次处理一个文件,可以像这样使用它,

for i = 1:length(inputNames)
    dataIn = load(fullfile('C:\data', inputNames{i}));
    dataOut = processData(dataIn);
    save(fullfile('C:\data', outputNames{i}), '-struct', 'dataOut');
end

or like this if you wanted to do all the loading, then all the processing, then all the saving - 或类似这样,如果您想进行所有加载,然后进行所有处理,然后进行所有保存-

for i = 1:length(inputNames)
    dataIn(i) = load(fullfile('C:\data', inputNames{i}));
end
for i = 1:length(inputNames)
    dataOut(i) = processData(dataIn);
end
for i = 1:length(inputNames)
    tmp = dataOut(i);
    save(fullfile('C:\data', outputNames{i}), '-struct', 'tmp');
end

One big advantage of writing processData in this way is that if you want to test or debug it, it is suddenly much easier. 以这种方式编写processData一大优势在于,如果您想测试或调试它,它会突然变得容易得多。 If the file loading/saving is inside the function, and you want to test it, you have to 如果文件加载/保存在函数内部,并且您想对其进行测试,则必须

  1. Create the test data 创建测试数据
  2. Save the test data to a file 将测试数据保存到文件
  3. Run processData with the filename as input 使用文件名作为输入运行processData
  4. Load the file containing the output data 加载包含输出数据的文件
  5. Check that the output data is correct 检查输出数据是否正确
  6. Remember to clean up the files containing the test data from wherever you put them 切记从任何地方清除包含测试数据的文件

If you separate the data loading/saving from the processing, then your testing procedure becomes 如果您将数据加载/保存与处理分开,那么您的测试过程将变为

  1. Create the test data 创建测试数据
  2. Run processData with the test data as input 以测试数据为输入运行processData
  3. Check that the output is correct 检查输出是否正确

Much simpler, and you didn't have to mess around with saving/loading files at any point, and you didn't create any messy files on your hard drive. 简单得多,而且您不必在任何时候都忙于保存/加载文件,也无需在硬盘驱动器上创建任何混乱的文件。 If you need to do any debugging, the process is also much easier - just create an example that your function will fail on, and then step through the code to see where it actually fails. 如果您需要进行任何调试,则过程也将更加容易-只需创建一个函数将失败的示例,然后逐步执行代码以查看其实际失败的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM