How to batch extract certain variables from individual workspaces (.mat), contained in individual files?

4 views (last 30 days)
Hello Community,
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.
Kind regards,
10B.
  1 Comment
10B
10B on 18 Mar 2016
I have found a few lines of code from something earlier I did to deal with the write to excel part. It will broadly follow:
%%Export data to Excel file
offset =1;
xlswrite('locations.xls', [newfolder X1 X2 Y1 Y2], 1, sprintf('A%d',offset));
offset = offset + 1;
and I think that should write everything in. Now, just need to work out the iterate through all the folders elements and extract variables from workspace...

Sign in to comment.

Answers (1)

Dave Behera
Dave Behera on 21 Mar 2016
Hi 10B,
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.
  1 Comment
10B
10B on 21 Mar 2016
Edited: 10B on 21 Mar 2016
Hello David,
I have put this together:
function readMatFile(folderName)
%readMatFile Function to copy in variables from .mat files and to write
%them to an excel spreadsheet
Import of data
% load in the speficic folder and desired variables
load(folderName, 'newfolder', 'X1', 'X2', 'Y1', 'Y2');
%%Set Offset
offset = 1;
%%Export data to Excel file
xlswrite('locations.xls', [newfolder, X1, X2, Y1, Y2], 1, sprintf('A%d',offset));
%%Send offset counter further on
offset = offset + 1;
end
and I've tested all the individual parts which work in the workspace... except at the point of writing to the excel sheet, as I get the following result:
0 0 0 1 ʊ ଢ ̠ ࣊
strange symbols and all! This result is obviously the filename, but not the required output of the variables.
Could you offer some further help please?
Regards,
10B.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!