Num2str without conversion character but with horizontal tab

9 visualizaciones (últimos 30 días)
Simon Aldworth
Simon Aldworth el 29 de Nov. de 2022
Editada: Stephen23 el 29 de Nov. de 2022
Hi,
I wish to convert an array to string type for export in a text file but I'm being thwarted by the formatSpec argument in num2str.
The data is currently double type and also contains NaNs. I borrowed a piece of code from elsewhere to get me going:
for i = 1:size(yout,1)
outputstring = [outputstring,sprintf('%s\n',num2str(yout(i,:),'%7.6e\t'))];
end
outputstring = strrep(outputstring,'NaN','');
The latter line means that importing the text file into the data processing application used by our team will result in a NOVALUE in place of the NaN. There is also code to collect together the channel names and units, with a horizontal tab between each entry, which works fine.
Ideally I would like the data to be exported without any conversion change so that the numerical format is left untouched and just a horizontal tab is inserted between each number.
Starting with the borrowed code above, I do get a tab between each value but I also get additional spaces (9 to be exact) adjacent to the NaN entries. This is a surprise because the help files say that num2str does not pad, so I seem to be misunderstanding something there. When the NaN entries are replaced by '' the other application doesn't 'see' these and loads the data as if zeros were there.
Thinking that it's the extra spaces that are causing the problem, I changed the code to try and perform no conversion change and only insert the tab. Unfortunately I can't find a piece of valid code to do that. The following are invalid:
outputstring = [outputstring,sprintf('%s\n',num2str(yout(i,:),'\t'))];
outputstring = [outputstring,sprintf('%s\n',num2str(yout(i,:),'%\t'))];
After reading another post, I tried the following conversion:
outputstring = [outputstring,sprintf('%s\n',num2str(yout(i,:),'%g\t'))];
This works on some lines but not others. Leading spaces are also present but the number varies in each row according to either the length of the first element (the time value) or perhaps the length of the longest element in the row (in the first row there are all zeroes except the NaN entries, and since these are three characters long, all entries except the first are three characters long). When strrep is used to remove the NaN it doesn't do so correctly as not all of the inserted tabs are left behind, causing the data import to be incorrect.
I don't mind if I'm forced to use a conversion so long as I can have a text file that looks like this:
channelname_1[tab]channelname_2[tab]...channelname_n[tab][newline]
units_1[tab]units_2[tab]...units_n[tab][newline]
value_1_1[tab]value_1_2[tab]...value_1_n[tab][newline]
value_2_1[tab]value_2_2[tab]...value_2_n[tab][newline]
where some of the values maybe NaN and should be replaced by a blank ''.
Thanks.
  1 comentario
Stephen23
Stephen23 el 29 de Nov. de 2022
The question is confusing: too much broken code, not enough actually explaining what you are trying to achieve. We know how to write broken code, what we don't know is exactly what you want. Some sample input and output data would help.
Your description needs clarification. For example, if you want lines like
value_1_1[tab]value_1_2[tab]...value_1_n[tab][newline]
then why are you specifying fieldwidths? Is this actually some attempt to align the data?

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 29 de Nov. de 2022
Editada: Stephen23 el 29 de Nov. de 2022
"...a text file that looks like this:"
channelname_1[tab]channelname_2[tab]...channelname_n[tab][newline]
units_1[tab]units_2[tab]...units_n[tab][newline]
value_1_1[tab]value_1_2[tab]...value_1_n[tab][newline]
value_2_1[tab]value_2_2[tab]...value_2_n[tab][newline]
We can do that:
% Fake data:
chn = {'ChOne','ChannelTwo','ChanThree'};
unn = {'millihelen','potrzebies','nanometer'};
mat = [11,12,NaN;21,NaN,23;31,32,33;NaN,42,43]
mat = 4×3
11 12 NaN 21 NaN 23 31 32 33 NaN 42 43
% data -> string, replace NaN with '':
fmt = [repmat('%.6e\t',1,numel(chn)),'\n'];
str = sprintf(fmt,mat.');
str = strrep(str,'NaN','');
% print to file:
fid = fopen('test.txt','wb');
fprintf(fid,'%s\t',chn{:});
fprintf(fid,'\n');
fprintf(fid,'%s\t',unn{:});
fprintf(fid,'\n');
fprintf(fid,'%s',str);
fclose(fid);
% check file content (ugh, tabs are ugly):
type test.txt
ChOne ChannelTwo ChanThree millihelen potrzebies nanometer 1.100000e+01 1.200000e+01 2.100000e+01 2.300000e+01 3.100000e+01 3.200000e+01 3.300000e+01 4.200000e+01 4.300000e+01

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by