split columns and save seperate .mat files
Mostrar comentarios más antiguos
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
el 3 de Jul. de 2019
Can you share your data.mat file?
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
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
el 3 de Jul. de 2019
It depends on how and what your data is. Attach your data.
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
el 3 de Jul. de 2019
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
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
el 3 de Jul. de 2019
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).
Respuesta aceptada
Más respuestas (1)
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
@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:
https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
and also why clear all is not recommended in code.
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..
@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
el 3 de Jul. de 2019
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!