How to batch extract certain variables from individual workspaces (.mat), contained in individual files?
1 ビュー (過去 30 日間)
I wonder if you can help with this problem. I have several hundred files all containing various Matlab files eg .fig etc., but only including one saved workspace in each folder as '.mat' files. I have looked all afternoon for a way of searching through all the child folders, extracting 4 variables from the saved workspaces, and then writing them all to an excel spreadsheet (I know I could use a struct - I'm just more comfortable with excel while I continue to learn Matlab!). All the child folders are named numerically, and this is the same name as the workspace in the folder. For example:
- Folder name: 1234, contains
- 1234.mat (the workspace)
- The workspace contains variables: X1 X2 Y1 Y2 (numeric) and newfolder (string)
- Excel spreadsheet named: locations.xlsx
What I would like to happen is the code reads the folder names in order, opens the workspace and extracts the variables, writes them to the excel sheet in the following order; newfolder X1 X2 Y1 Y2, and then moves on to the next folder and repeats the process until all child folders are done.
I would usually post the code I have tried to make but nothing is worth showing as its not even close. I have not used Matlab for a couple of months now and it seems like everything I had learnt before has rapidly disappeared from my brain, so I am rather frustratingly making the same old mistakes again!
If you can offer any help or just pointers to other answers, I would really appreciate the input. Thanks.
回答 (1 件)
Dave Behera 2016 年 3 月 21 日
Here are a few pointers to help you write your code:
1. You can create a new function called ReadMatFile() which takes as input a folder name ,the path to the xls file and the offset from where to start writing. It should read the .mat file in the folder and use xlswrite to update it as you showed in your code.
2. The path to the .mat file should be a concatenated list of strings: filename = [foldername '\' foldername '.mat']; % Replace \ with / for Linux Here, foldername is a the name of the folder you will give as input to the function.
3. Use the above string to load the .mat fie with 'load': http://www.mathworks.com/help/matlab/ref/load.html
This will load all the variables into the workspace, which you can then write to your .xls file.
4. You will need to call the function this way for each folder in your list of folders using a for loop.