MATLAB Answers

split columns and save seperate .mat files

39 ビュー (過去 30 日間)
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 件のコメント

表示 8 件の古いコメント
Guillaume
Guillaume 2019 年 7 月 3 日
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 2019 年 7 月 3 日
see attached, I want to save each of those columns in a seperate .mat file
Stephen Cobeldick
Stephen Cobeldick 2019 年 7 月 3 日
"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).

サインイン to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 7 月 3 日
編集済み: Stephen Cobeldick 2019 年 7 月 3 日
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 件のコメント

表示 1 件の古いコメント
Stephen Cobeldick
Stephen Cobeldick 2019 年 7 月 3 日
"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 2019 年 7 月 3 日
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
Stephen Cobeldick
Stephen Cobeldick 2019 年 7 月 3 日
@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,:));

サインイン to comment.

その他の回答 (1 件)

Shameer Parmar
Shameer Parmar 2019 年 7 月 3 日
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 件のコメント

表示 1 件の古いコメント
Shameer Parmar
Shameer Parmar 2019 年 7 月 3 日
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 2019 年 7 月 3 日
@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 2019 年 7 月 3 日
@Shameer, thank you for helping me out! I am very new to this so I will go for the simpler version. Thank you!

サインイン to comment.

サインイン してこの質問に回答します。


Translated by