How to save variables to a specific cell in a .mat file?

9 views (last 30 days)
I have a .mat file, called Threshold_values.mat, which consists of an array (68x1) and just contains cluster1, cluster2, cluster3 etc. in each cell.
I use a GUI to look at each cluster individually and calculate 6 different variables.
I need to write these 6 variables alongside the cluster name in different columns. How can I save these into the .mat file and keep them there?
I am already using strcmp to know which row I need to write to in the file.
  2 Comments
Adam
Adam on 2 Dec 2015
It would probably help if you give a simplified sample of your actual data - it's hard to visualise exactly what structure you have and are aiming for otherwise.
A mat file can store many variables at once though so you do not need to be constrained by trying to crowbar data into an existing variable where it doesn't necessarily fit very well.
Jack Miskell
Jack Miskell on 2 Dec 2015
Edited: Walter Roberson on 2 Dec 2015
Sorry I'm still fairly new to matlab so I'm probably not making much sense.
So I have a .mat file which I will eventually want to be a 68x7 matrix.
Titles of each column as follows;
Clustername, minPV, maxPV, ThreshPV, minArea, maxArea, ThreshArea
So right now I have the cluster names already set up in the clustername column. My code uses a GUI which calculates the 6 variables in the other columns for each cluster. I want to write these 6 variables to the corresponding row and column for each cluster. I use strcmp to find out which row I need to write to, my only problem is knowing how to actually save the variables to the corresponding cell in the .mat file (and actually stay there).
This seems like it should be fairly straight forward, but maybe I'm not explaining myself very well.
Thank you for getting back to me so quickly Adam!

Sign in to comment.

Accepted Answer

Adam
Adam on 2 Dec 2015
Edited: Adam on 2 Dec 2015
By far the easiest solution, generally speaking, is to just load the whole matrix in from the mat file and then you just write to the indices you have worked out, then save the whole thing back again to file.
Don't try to update live to file unless you have a good reason to do so. If you had a vast data set then this might be the case and a matfile approach (note that 'matfile' is distinct from a normal mat file, but you don't need to bother about the distinction for your size of data set) would likely be sensible, but 68x7 is a very small data set that has no problem being loaded into memory.
I think since you are new to Matlab you are mixing up some terminology, so just a few points for clarification (hopefully):
  • A .mat file is simply a Matlab file type into which you can basically save a whole workspace - i.e. any number of different variables of whatever type you wish - it is not limited to being a single entity and so isn't like if you were trying to write your data into a binary or text file.
  • A 'cell' in Matlab is generally used to refer to an element of a cell array which is a special type of array in which you can store a large variety of different data types. As an ideal to aim for you should only use cell arrays when you really need to. For raw data use a standard array - its manipulation is far easier because its whole purpose is to store numeric data to do operations on.
  • When you talk about 'titles' of columns I'm not 100% sure what structure you have, but if you have a cell array for your entire data purely because you want one row of titles followed by 68 rows of pure numeric data then don't do this!! There is a 'table' structure in Matlab which you can learn about, but for something this simple you can also simply store your column titles in a separate variable as e.g.
columnTitles = { 'Clustername', 'minPV', 'maxPV', 'ThreshPV', 'minArea', 'maxArea', 'ThreshArea' };
then your data can be stored in a standard numeric array e.g.
data = zeros( 68, 7 );
You can then search the column titles array for your indices (probably in the same way you do now) and use these for the columns of the numeric matrix.
This is essentially like a table except that you have to manually ensure that your array of titles is the same length as you have columns of data so there is a little more manual overhead than using
doc table
On the other hand it is simpler for a beginner I would think.
  1 Comment
Jack Miskell
Jack Miskell on 3 Dec 2015
Thank you for your help Adam. I had a look at tables and this does the job for me.
Also thank you for clearing up a few terminologies. You've been a massive help!

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!