split columns and save seperate .mat files
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marlou Lasschuijt
am 2 Jul. 2019
Bearbeitet: Stephen23
am 3 Jul. 2019
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 Kommentare
Stephen23
am 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).
Akzeptierte Antwort
Stephen23
am 3 Jul. 2019
Bearbeitet: Stephen23
am 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);
save(F,'V')
end
4 Kommentare
Stephen23
am 3 Jul. 2019
Bearbeitet: Stephen23
am 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,:);
...
save(...,'L')
You might even want to remove the whitespace, e.g.:
L = strtrim(S.labels(k,:));
Weitere Antworten (1)
Shameer Parmar
am 3 Jul. 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 Kommentare
Guillaume
am 3 Jul. 2019
Bearbeitet: Guillaume
am 3 Jul. 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.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!