1. 程式人生 > >Matlab——資料操作小記

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