Write a well tabulated txt file

1 次查看(过去 30 天)
Emilio Pulli
Emilio Pulli 2021-12-4
编辑: DGM 2021-12-4
This is my code:
v_cutin=2.5;
v_cutout=15;
v_rated=5.5;
v=linspace(v_cutin,v_cutout,15);
omega_rpm=linspace(1,600,200);
dataset_lin=importdata('dataset_lin.mat');
dataset_red_lin=NaN*ones(((length(omega_rpm)/10)+1)*length(v)+length(v)-1,6);
x=1;
row=0;
y=1;
stop=0;
for i=1:length(dataset_lin)
if isnan(dataset_lin(i,1))==0
row=row+1;
stop=0;
elseif isnan(dataset_lin(i,1))==1 && stop<1
step=floor(row/10);
check=mod(row,10);
if check==0
dataset_red_lin(y:1:y+9,:)=compose("%.3f",dataset_lin(x:step:(i-1),:));
dataset_red_lin(y+10,:)=NaN*ones(1,6);
x=i+1;
row=0;
y=y+11;
elseif check>0
dataset_red_lin(y:1:y+10,:)=compose("%.3f",dataset_lin(x:step:(i-check),:));
dataset_red_lin(y+11,:)=NaN*ones(1,6);
x=i+1;
row=0;
y=y+12;
end
stop=stop+1;
elseif stop==1
i=length(dataset_lin);
end
end
A = dataset_red_lin;
C = num2cell(A);
idx = isnan(A);
C(idx) = {''};
writematrix(string(C), 'dataset_red_lin.txt', 'Delimiter', 'tab');
I need the conversion to cell in order to replace the NaN values with blank spaces. However, the resultant txt file is not well tabulated because there are some numbers which has no decimal digits. Ho do I can figure out the problem?
  4 个评论
Emilio Pulli
Emilio Pulli 2021-12-4
The code that I typed takes a matrix as input (dataset_lin.mat) which is composed of several submatrixes separated by NaN rows and with variable number of rows. For instance, the first submatrix occupies the rows 1 to 31, at row 32 I have a NaN row and from row 33 to 112 I have another submatrix and so far so forth. I have a total of 15 different submatrixes. The code takes this input matrix, for each submatrix store only 11 equally spaced rows. Afterthat, it converts the new shorter matrix containing submatrixes of equal dimension into cell arrays in order to replace the NaN rows with blank spaces. Finally, the final obtained matrix, with submatrixes of equal dimenion and separated by blank spaces, is written on a txt file. However, the resultant txt file does not have aligned colums...
Voss
Voss 2021-12-4
Maybe try a fixed-width format containing enough digits for your data, in your calls to compose, e.g., compose("%9.3f",___)

请先登录,再进行评论。

回答(1 个)

DGM
DGM 2021-12-4
编辑:DGM 2021-12-4
As @Benjamin suggests, you can use fixed field widths in your output.
dataset_lin=importdata('dataset_lin.mat');
stepsize0 = 3; % initial row step size
blocksize = 11; % number of rows per output block
% find block extents
nanrows = find(all(isnan(dataset_lin),2));
nanrows(find(diff(nanrows)==1)+1) = [];
nanrows = [0; nanrows];
% process blockwise
fid = fopen('testfile.txt','w');
for b = 2:numel(nanrows)
thisblock = dataset_lin(nanrows(b-1)+1:stepsize0:nanrows(b)-1,:);
thisblock = thisblock(1:blocksize,:);
fprintf(fid,'%6.3f\t%8.3f\t%5.3f\t%5.3f\t%8.3f\t%7.3f\n',thisblock.');
fprintf(fid,'\n');
stepsize0 = stepsize0 + 1;
end
fclose(fid);

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by