Matlab——数据操作小记

 数据写入CSV、TXT

数字转字符
B = num2str(A);
B = strcat(B,'%');
数据转包才能写入csv
b=cellstr(a);
num = ones(3,3);
str1 = {'a','b','c'}
numcell = num2cell(num);      %将矩阵的每个数单独做一个cell小单元,matalb版本低可能不支持这个函数
data = [numcell;str1];
cell2csv('testdata.csv',data);
使用csv存储数据
csvwrite('Output.csv',data);       
打开清除txt、写入到txt文件中
fopen('w.txt','wb')
dlmwrite('s.txt',data,'-append','delimiter','\t','newline','pc')

数据格式转换

数字转字符
B = num2str(A);

字符拼接
B = strcat(B,'%');

字符转cell
b=cellstr(a);

数值转cell
num = ones(3,3);
str1 = {'a','b','c'}
numcell = num2cell(num);  

数据类型 Cell 操作

Data = cell(行,列);

Data{row,col}

isempty(Data{i,j}; % 返回包指定位置的空(1)或有值(0)

% cell转csv,只能写入一层数据,第一层数据后不能直接写入
% cell2csv子函数(如下)
% 使用:cell2csv('*.csv',Data);

%存储为cell2csv.m 调用即可
function cell2csv(fileName, cellArray, separator, excelYear, decimal)
% % Writes cell array content into a *.csv file.
% % 
% % CELL2CSV(fileName, cellArray[, separator, excelYear, decimal])
% %
% % fileName     = Name of the file to save. [ e.g. 'text.csv' ]
% % cellArray    = Name of the Cell Array where the data is in
% % 
% % optional:
% % separator    = sign separating the values (default = ',')
% % excelYear    = depending on the Excel version, the cells are put into
% %                quotes before they are written to the file. The separator
% %                is set to semicolon (;)  (default = 1997 which does not change separator to semicolon ;)
% % decimal      = defines the decimal separator (default = '.')
% %
% %         by Sylvain Fiedler, KA, 2004
% % updated by Sylvain Fiedler, Metz, 06
% % fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
% % added the choice of decimal separator, 11/2010, S.Fiedler
% % modfiedy and optimized by Jerry Zhu, June, 2014, [email protected]
% % now works with empty cells, numeric, char, string, row vector, and logical cells. 
% % row vector such as [1 2 3] will be separated by two spaces, that is "1  2  3"
% % One array can contain all of them, but only one value per cell.
% % 2x times faster than Sylvain's codes (8.8s vs. 17.2s):
% % tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;

%% Checking for optional Variables
if ~exist('separator', 'var')
    separator = ',';
end

if ~exist('excelYear', 'var')
    excelYear = 1997;
end

if ~exist('decimal', 'var')
    decimal = '.';
end

%% Setting separator for newer excelYears
if excelYear > 2000
    separator = ';';
end

% convert cell
cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false);

%% Write file
datei = fopen(fileName, 'w');
[nrows,ncols] = size(cellArray);
for row = 1:nrows
    fprintf(datei,[sprintf(['%s' separator],cellArray{row,1:ncols-1}) cellArray{row,ncols} '\n']);
end    
% Closing file
fclose(datei);

% sub-function
function x = StringX(x)
    % If zero, then empty cell
    if isempty(x)
        x = '';
    % If numeric -> String, e.g. 1, [1 2]
    elseif isnumeric(x) && isrow(x)
        x = num2str(x);
        if decimal ~= '.'
            x = strrep(x, '.', decimal);
        end
    % If logical -> 'true' or 'false'
    elseif islogical(x)
        if x == 1
            x = 'TRUE';
        else
            x = 'FALSE';
        end
    % If matrix array -> a1 a2 a3. e.g. [1 2 3]
    % also catch string or char here
    elseif isrow(x) && ~iscell(x)
        x = num2str(x);
    % everthing else, such as [1;2], {1}
    else
        x = 'NA';
    end

    % If newer version of Excel -> Quotes 4 Strings
    if excelYear > 2000
        x = ['"' x '"'];
    end
end % end sub-function
end % end function

猜你喜欢

转载自blog.csdn.net/weixin_41275726/article/details/85207603