欢迎来这里逛逛,前些时间我的空间总是跟别人的重叠,搞得我也没心思来这里打理,今后可能好一点儿^_^
[转贴]:mathswork上的一个把cell数组(允许中文)写到文本中的程序
查看( 156 ) /
评论( 3 )
TAG:
-
damao_pq发布于2006-10-14 10:57:30
-
原贴中输入变量trennZeichen不起作用
稍改了一下
function cell2csv(datName,cellArray,trennZeichen)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(datName,cellArray,trennZeichen)
%
% datName = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% trennZeichen = seperating sign, normally:',' (it's default)
%
% by Sylvain Fiedler, KA, 2004
if nargin<=2
trennZeichen = ' ';
end
datei = fopen(datName,'w');
for z=1:size(cellArray,1)
for s=1:size(cellArray,2)
var = eval(['cellArray{z,s}']);
if size(var,1) == 0
var = '';
end
if isnumeric(var) == 1
var = num2str(var);
end
fprintf(datei,var);
if s ~= size(cellArray,2)
fprintf(datei,trennZeichen);
end
end
fprintf(datei,'\n');
end
fclose(datei);
-
bainhome 发布于2006-10-14 17:35:39
-
再次修改,应该比二楼稍微灵活一点儿,程序见一楼
[ 本帖最后由 bainhome 于 2006-10-15 05:14 编辑 ]
-
dingzixiu 发布于2007-01-19 16:44:19
-
也许bainhome斑竹早解决了这个小问题,刚刚搜索你发的文章,拜读的过程中,顺便贴出我的一点改进。后面的换行在windows notepad中用\r\n才会换行,不过在matlab中的editor可以用\n即可识别为换行了。
function read_csv_plus_text(t,num)
% 使用cell2csv读取文件的示例程序
%
clc
c=cell(4);
str=' ';
for i=1:t+2
str=[str,' '];
end
for i=2:4
for j=1:4
c{i,j}=rand(1);
c{1,j}=['测试',num2str(j),str];
end
end
% num=10;%your precision number!
cell2csv('ttt.txt',c,t,num);
system('ttt.txt') %invoke notepad to open the text file.
function cell2csv(datName,cellArray,trennZeichen,num)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(datName,cellArray,trennZeichen,precisionNum)
%
% datName = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% trennZeichen = the number of blankspaces,and it has to be either
% great than zero or a positive interger.
%num =the number of the save numerical precision, for example
%num=2--->0.24,num=5--->0.12345
% by Sylvain Fiedler, KA, 2004
% modified by bainhome,XJ,2006
if trennZeichen<=0|round(trennZeichen)~=trennZeichen
errordlg('trennZeichen has to be either great than zero or a positive interger.',...
'输入错误对话框')
return
else
str=' ';
for i=1:trennZeichen-1
str=[str,' '];
end
datei = fopen(datName,'w');
for z=1:size(cellArray,1)
for s=1:size(cellArray,2)
var = eval(['cellArray{z,s}']);
if size(var,1) == 0
var = '';
end
if isnumeric(var) == 1
format=['%.',num2str(num),'f'];
var=sprintf(format,var);
end
fprintf(datei,var);
if s ~= size(cellArray,2)
fprintf(datei,str);
end
end
fprintf(datei,'\r\n');
end
fclose(datei);
end
[ 本帖最后由 dingzixiu 于 2007-1-19 16:45 编辑 ]
标题搜索
日历
|
|||||||||
| 日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
| 1 | 2 | 3 | |||||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
| 11 | 12 | 13 | 14 | 15 | 16 | 17 | |||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | |||
| 25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
我的存档
数据统计
- 访问量: 4150
- 日志数: 26
- 建立时间: 2006-05-20
- 更新时间: 2007-11-11


