I run my scripts which produce hundreds or thousands of .mat files. Each of this file contains two variables: resultsU and resultsT. I want to append the files but not overwrite the variables. What is the easiest way to do so in Matlab? Some suggest manual manipulation of the .mat file which is not easy or efficient to do when you have hundreds of .mat files.
How to append multiple .mat files that have the same variable names in them?
1.8k views Asked by JustCurious At
2
There are 2 answers
9
rayryeng
On
It's actually a lot easier than you think. If you want to append to MAT files, simply use save with the -append flag. Assuming that you have a few variables... let's call them p and q, and assuming you have a file called test.mat, it's very simply:
save('test.mat','p','q','-append');
The beauty of this is that you don't need to load any of the variables in the MAT file and resave them with the appended variables. This appends the desired variables into the MAT file without you ever having to load them into MATLAB.
If you have a bunch of .mat files in a directory, you can do something like this:
folder = '...'; %// Place directory here
f = dir(folder); %// Find files
%// For each file...
for idx = 1 : numel(f)
name = fullfile(folder, f(idx).name); %// Get path to file
%// Do some processing
%//...
%//
%// Append to file
save(name, ..., ..., ..., ..., '-append');
end
What goes inside the ... for save are the variables you want to append to each file.
Related Questions in MATLAB
- How to open and read video stream in Matlab
- Interpolation and replace zeroes
- How can I fix my code to do line by line conditional statements in Matlab
- matlab crash during acquisition of pointgrey images
- Calling text file
- Apply gaussian filter on text
- re-plotting of data on same GUI axes in matlab
- Issue with nume1 in MATLAB
- Multiply two variables in Matlab with vpa - high precision
- ODE - Solving Parameter Dependent on Variable [Matlab]
- Need help in detecting multiple blobs
- How does TIC TOC in matlab work?
- Image based steganography that survives resizing?
- lowering computaional cost in finding the location of minimum distance
- FFT Filtering of signal
Related Questions in VARIABLES
- Simplexml get path from variable
- Python - Dynamically naming variables
- Azure Web App PATH Variable Modification
- How can I pass variable to ansible playbook in the command line?
- Fetching URL vars into form and submitting to other page
- Iframe not passing url parameters
- Stata: Retrieve variable label in a macro
- How to make all variables in javascript script have two decimal points
- Javascript works with HTML but not in Rails
- Running Line of code twice causes activeX error 0x8000ffff
- Erlang syntax error unclear
- CMD specifying columns to save?
- Simplify code with sending Methods as Variable
- Calling a variable in Matlab without using the full name?
- How to call c-style cleaner functions implicitly?
Related Questions in SAVE
- Trying to save an np array with string and floats, but getting a error
- saving matlab file (.mat) with dynamic name
- Saving Data form in CakePHP
- Python - Pickle init takes 4 arguments - 1 given
- Losing values after reading from file and saving np array to the same file
- Android: Intentionally Blank Lines in EditText Are Not Getting Saved
- How to save cell value(image) of a vertical grid control (devexpress) to sql database?
- Open in same view controller as closed on (swift)
- MvvmCross on iOS - Strategies for Saving and Restoring State
- How to make a save button
- C# - Settings.Default.Save() is very slow
- Saving text in labels
- Python: Can you Save an Object and all of it's Associated Values?
- Implementing both Serializable and Parcelable interfaces from an object in Android - conflict
- Disable Excel save option but allow macro save
Related Questions in LOAD
- What are the file formats that read into R the fastest?
- How can I send a integer from my Java file to my XML folder? (Android Studio)
- Delay on javascript loading
- How to load image from file into the memory in android?
- python2.7 select data file to rerun file
- Gatling - show failed requests only after max retry
- Read next line when loading file in Common Lisp
- How to load a owl file to neo4j using eclipse
- How can I load an image from a local directory in PHP?
- Python: Can you Save an Object and all of it's Associated Values?
- Is there a method so you'd find out which/name of functions that were being called?
- MySQL is adding characters to my data
- Scheduling each requests in a jmeter threadgroup
- Using Registry to install Excel AddIn
- Ajax load dynamic page
Related Questions in MAT
- optimized copying from an openCV mat to a 2D float array
- conversion between Mat and Mat1b/Mat3b
- Calculating sum of a subset of Mat OpenCV
- Sending a Mat object over socket from Java to Java
- ArcView (ArcGIS) AVL file format to Matlab mat file format
- read pixel values of a binary image in opencv
- How to convert an Image to Mat Android
- Error on creating a wrapper class for Mat Object (Getting “multiple of channels count” error)
- How to append multiple .mat files that have the same variable names in them?
- Loading multiple .mat files containing the same variable name and changing the variable names simultaneously?
- What is meant by header of a Mat object in OpenCV?
- Different channels in opencv
- OpenCV SVM - How to change Mat object to appropriate dimensions?
- C++ OpenCV create Mat from SIGNED char array (not unsigned)
- Process AVFrame using opencv mat causing encoding error
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
rayryeng's answer is good if running the code which created the files is an option. However, if working with a huge amount of files is a given fact which you need to deal with, I would suggest to go about this using and array of
structs(which resemblesstructconcatenation).Consider the following example function:
What it does is to combine
.matfiles containing the same variable names. Now you can runcombined_results = CombineMat(folder_with_mat_files)and get astructwhich contains all the different results (assuming you have enough memory to hold them). After you have thisstructin memory, you can save it to a single.matfile.Note 1: If you don't have enough memory to load all the files, you can add another piece of code to
CombineMatthat dumpscombined_resultsto disk and clears it after a certain amount of loop iterations (possibly with the'-append'option as suggested by rayryeng. This personally doesn't make sense to me if OOM happens, because then you would have problems loading the resulting file :)Note 2: Obviously, if you want your result as something other than an array of
structs, the code needs to be modified accordingly.In a more general case, where you are trying to concatenate structures with different variable names, you can use the following FEX submission (which I can recommend from personal experience!).
P.S. My code was written on MATLAB 2015a.