split columns and save seperate .mat files

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 '
units:
val =
'Volts '
'cm H20'
'Volts '
'mV '
Do you know how to do this?
Thank you in advance
Marlou

11 comentarios

Raj
Raj el 3 de Jul. de 2019
Can you share your data.mat file?
Rik
Rik el 3 de Jul. de 2019
This problem has two parts: splitting the variables, and saving them. Which of the two is giving you problems?
Stephen23
Stephen23 el 3 de Jul. de 2019
Marlou Lasschuijt's incorrectly accepted "Answer" moved here:
Hi Rik,
Both are the problem I dont know how to do this.
@ Raj I am not able to share the data because the file is too big even as a .zip file to be uploaded.
Thank you in advance
KSSV
KSSV el 3 de Jul. de 2019
It depends on how and what your data is. Attach your data.
Shameer Parmar
Shameer Parmar el 3 de Jul. de 2019
I have question here..
So your .mat file includes 4 variables (each with one cloumn data) OR a single variable with 4 coulmn data ?
I can provide you the solution, but its depend on your answer...
Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
it is 1 .matfile with 4 columns ( many rows). Each column is a variable that I would like to save as a seperate .mat file
Stephen23
Stephen23 el 3 de Jul. de 2019
"it is 1 .matfile with 4 columns ( many rows). Each column is a variable that I would like to save as a seperate .mat file "
This is unclear, because .mat files contain variables, not columns. Variables, if they are arrays, have columns. Please either state clearly if your .mat file contains one variable or four variables, or even better, simply upload your .mat file by clicking the paperclip button.
Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
Four variables.
I cannot upload the file as it is too large even as a zip file but attached a screenshot.
Guillaume
Guillaume el 3 de Jul. de 2019
Editada: Guillaume el 3 de Jul. de 2019
The screenshot shows a workspace with 6 variables, and one of these variables, data has 4 columns. It does not show any mat file.
It sounds like you want to save that data variable, with headers. Do you want to save it as a .mat file (which can only be opened in matlab) or do you want actually want to save it as something, maybe a text file or an excel file?
Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
see attached, I want to save each of those columns in a seperate .mat file
Stephen23
Stephen23 el 3 de Jul. de 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).

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 3 de Jul. de 2019
Editada: Stephen23 el 3 de Jul. de 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);
save(F,'V')
end

4 comentarios

Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
Thank you that worked perfectly. the ; after save misses but it worked! Can I specify the names for each column (column 1 = heart rate).
Best,
Marlou
Stephen23
Stephen23 el 3 de Jul. de 2019
Editada: Stephen23 el 3 de Jul. de 2019
"Can I specify the names for each column (column 1 = heart rate)."
Possibly, it depends on what you mean:
  • do you want to change the variable name (if yes, then you can do this by defining the fields of a structure and saving that).
  • do you want to adda header to each column (if yes, then you would need to use a table).
Please clarify which one you want, and also specify where those names should come from (the words "heart rate" do not exist anywhere in your sample .mat file).
Note that in both of those cases there is little advantage in saving the four columns in four .mat files, they could just as easily be saved in one .mat file (i.e. in one table or one structure).
Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
I want to save the column by the name of the label of that column.
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.
The next program I am using does not except the data without the label.
thank you for all your help.
Best,
Marlou
Stephen23
Stephen23 el 3 de Jul. de 2019
Editada: Stephen23 el 3 de Jul. de 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,:);
...
save(...,'L')
You might even want to remove the whitespace, e.g.:
L = strtrim(S.labels(k,:));

Iniciar sesión para comentar.

Más respuestas (1)

Shameer Parmar
Shameer Parmar el 3 de Jul. de 2019
clc;
clear all;
% Loading your .mat file (replace data.mat with your .mat fileName)
load('data.mat');
% 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

4 comentarios

Stephen23
Stephen23 el 3 de Jul. de 2019
Editada: Stephen23 el 3 de Jul. de 2019
@Shameer Parmar: Rather than teaching bad practices (i.e. loading directly into the workspace and then using eval to access the variables), you should show beginners how simple it is to write neat and efficient code, e.g. by loading into an output variable:
S = load(...)
and then accessing its fields:
Note that this answer is extremely fragile because it depends on the clear to provide an empty workspace, which is required for the who output indexing to work properly. Removing the clear will provide unexpected results.
Note that copy-and-pasting code is a sign that you are doing something wrong. In particular, you should be using a loop and getting the computer to do the repetition efficiently for you.
I am sure that you have been reading this forum and the MATLAB documentation, so that you know by now why accessing variable names dynamically should be avoided:
and also why clear all is not recommended in code.
Shameer Parmar
Shameer Parmar el 3 de Jul. de 2019
I am just trying to help others.. I dont know what is your problem...
If you already know the answer of such question then please try to help others..
If you already know the simple way then please try answering to the question instead of commenting.. so if you answer in the simple way then I will also get the notification and I will also unerstand it..
I hope you understand my comment... Thanks for your understanding..
Guillaume
Guillaume el 3 de Jul. de 2019
Editada: Guillaume el 3 de Jul. de 2019
@Shameer, sorry but your answer is not good at all. While it may solve the immediate problem, at the same time you're teaching beginners extremely bad matlab practices which will cause a lot more problems in the future.
As Stephen said, any code that uses eval is bad code (I'm sorry to say I don't think you're experienced enough to use eval safely).
As for answering the question, I'm sure Stephen will provide an answer as soon as the question is made clear. The statement by the OP that "it is 1 .matfile with 4 columns" is meaningless, as mat files do not have columns. So, most likely the file is not even a mat file (or the columns are not columns but something else). There's no point in giving an answer when we don't have all the details to answer properly.
Marlou Lasschuijt
Marlou Lasschuijt el 3 de Jul. de 2019
@Shameer, thank you for helping me out! I am very new to this so I will go for the simpler version. Thank you!

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 2 de Jul. de 2019

Editada:

el 3 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by