split columns and save seperate .mat files

38 views (last 30 days)
Dear Reader,
I would like to save each column (4 columns) of a data.mat file as a seperate .mat file including the units, labels ISI ISI_units_start_sample
labels :
'PPG100C '
'TSD160A - Differential Pressure, 2.5 cm H20, DA100C'
'Scanner Trigger '
'ECG100C '
val =
'Volts '
'cm H20'
'Volts '
'mV '
Do you know how to do this?
Thank you in advance
Stephen23 on 3 Jul 2019
"I want to save each of those columns in a seperate .mat file "
There are actually six variables in your uploaded .mat file: are you referrring to the columns of the variable named data ? If yes, then that is what my answer does (simpler and much more robustly than the accepted answer).

Sign in to comment.

Accepted Answer

Stephen23 on 3 Jul 2019
Edited: Stephen23 on 3 Jul 2019
Simpler, much more robust, much more versatile code (without bad code practices such as dynamically accessed variable names, loading directly into workspace, copy-and-pasting code...).
Apparently you want to access the columns of one array, which is named data and is saved in a .mat file, and save each of its columns in their own .mat file. This is easy to do:
S = load('Marlou.mat');
for k = 1:size(S.data,2);
V = S.data(:,k);
F = sprintf('column_%d.mat',k);
Stephen23 on 3 Jul 2019
Edited: Stephen23 on 3 Jul 2019
@Marlou Lasschuijt: as I understand it, you want to save the four columns using the names given in the character arrray labels (note that none of those labels matches your example "heart rate"). There are ways to do this (e.g. using a structure, as I wrote), but you will need to consider what happens when the label is not a valid field/variable name, e.g. the second row of the character array labels is:
TSD160A - Differential Pressure, 2.5 cm H20, DA100C
This cannot be used as a fieldname or a variable name. You could replace all of the invalid characters with some other character/s, but only you can decide what is appropriate. Please advise what you really mean, because nowhere in your data is there any "label" that is suitable for naming fields/variables with.
Personally I would not force such meta-data into a fieldname or a variable name, because doing so makes code slow, complex, liable to bugs, and hard to debug.
"Besides that I need to save the label information belonging to that column in the new file, I lost that when I used your script."
That is easy: I am sure that you can see how to use indexing and follow the same logic that my answer uses, and obtain the corresponding rows of the labels variable:
L = S.labels(k,:);
You might even want to remove the whitespace, e.g.:
L = strtrim(S.labels(k,:));

Sign in to comment.

More Answers (1)

Shameer Parmar
Shameer Parmar on 3 Jul 2019
clear all;
% Loading your .mat file (replace data.mat with your .mat fileName)
% capturing workpace data
workspaceVar = who;
Var = eval(workspaceVar{1});
% storing column1
column1 = Var(:,1);
% saving column1 to new .mat file
save Column1.mat column1;
% storing column2
column2 = Var(:,2);
% saving column2 to new .mat file
save Column2.mat column2;
% storing column3
column3 = Var(:,3);
% saving column3 to new .mat file
save Column3.mat column3;
% storing column4
column4 = Var(:,4);
% saving column4 to new .mat file
save Column4.mat column4;
% You can give proper .mat file name which you want to create in above 'save' command
Marlou Lasschuijt
Marlou Lasschuijt on 3 Jul 2019
@Shameer, thank you for helping me out! I am very new to this so I will go for the simpler version. Thank you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!