我有一个.mat文件,数据结构如下。
数据=
带字段的结构:
axis1: [25626600×1 double]
axis2: [25626600×1 double]
axis3: [25626600×1 double]
datenum: [25626600×1 double]
sample_rate: 30
serial_number: 'MOS2D10171145'
我想把这个.mat文件另存为csv文件。
我尝试了这个代码,但它给了我这个错误。
csvwrite('FileName.csv', Data);
Undefined function 'real' for input arguments of type 'struct'.
Error in dlmwrite (line 189)
str = sprintf('%.*g%+.*gi',precn,real(m(i,j)),precn,imag(m(i,j)));
Error in csvwrite (line 42)
dlmwrite(filename, m, ',', r, c);
正如Wolfie所解释的,您不能直接执行此操作,因为您的sample_rate和serial_number变量不能在CSV中直接执行。
一个解决方案可以是将这2个变量放在CSV文件名中以保留它们。
%% Parameters
cdSAVE='YOUR_PATH'; % PUT YOUR SAVING PATH
%% Variables (put your own)
axis1 = zeros(250,1);
axis2 = zeros(250,1)+1;
axis3 = zeros(250,1)+2;
datenum = zeros(250,1)+3;
sample_rate = 30;
serial_number = 'SERIAL_NUMBER';
%% Make a matrix with what you can
Matrix = zeros(length(axis1),4);
Matrix(:,1)=axis1;
Matrix(:,2)=axis2;
Matrix(:,3)=axis3;
Matrix(:,4)=datenum;
%% Convert to table (to have the headers written in your csv file)
T = array2table(Matrix);
T.Properties.VariableNames(1:4) = {'axis1','axis2','axis3','datenum'};
%% Create a the filename with the 2 other datas
fileName = char(strcat('Extract_CSV_Sample_rate_',string(sample_rate),'_Serial_Number_',serial_number,'.csv'));
%% Writing
fullPathFileName=char(strcat(cdSAVE,"",fileName));
%% Choose what you want :
%% Here for just the matrix with csvwrite
csvwrite(fullPathFileName,Matrix);
%% Here with headers also with writetable
writetable(T,fullPathFileName)
CSV名称将为:Extract_CSV_Sample_rate_30_Serial_Number_Serial_Number.CSV