format data for floating to integer to ascii

13 views (last 30 days)
i have data input in edit text like this :
B2
4
3
0
10000
M12
3
1
1
1000
0
240
120
1520
0
but when i use save ascii like this command :
save(red.txt,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','-ASCII'),
the variable from input using
b=str2num(get(edit2,'String'));
and edit text at least 16
edit1=uicontrol('parent',win1,...
'units','points',...
'position',[130 200 30 15],...
'backgroundcolor',[1 1 1],...
'style','Edit',...
'string','',...
'fontname','arial',...
'fontsize',10);;
i get this :
6.6000000e+01 5.0000000e+01
4.0000000e+00
3.0000000e+00
0.0000000e+00
1.0000000e+03
7.7000000e+01 4.9000000e+01 5.0000000e+01
3.0000000e+00
1.0000000e+00
1.0000000e+00
1.0000000e+03
0.0000000e+00
2.4000000e+02
1.2000000e+02
1.5200000e+03
0.0000000e+00
0.0000000e+00
how do you make the same as the input?

Answers (2)

Walter Roberson
Walter Roberson on 30 Oct 2015
Values you get from the String property of a uicontrol() are always strings until you convert them to numeric. Just write them out as strings.
b = get(edit2,'String');
fid = fopen('red.txt', 'wt');
fprintf(fid, '%s\n%s\n', a, b);
fprintf(fid, '%d\n%d\n%d\n', c, d, e);
fprintf(fid, '%s\n', f);
... and so on
fclose(fid);
  3 Comments
Muhammad Habibie
Muhammad Habibie on 31 Oct 2015
Edited: Walter Roberson on 31 Oct 2015
And if I wanted to add delimited tab next to B2 for additional description next to it.
and i try your script Mr Walter like this :
fid = fopen(savefile, 'wt');
%fprintf(fid, '%s\n%s\n', a);
fprintf(fid, '%s\n', a);
fprintf(fid, '%d\n', b);
fprintf(fid, '%d\n%d\n%d\n', c, d, e);
fprintf(fid, '%s\n', f);
fprintf(fid, '%d\n%d\n%d\n', f, g, h);
fprintf(fid, '%d\n%d\n%d\n', i, j, k);
fprintf(fid, '%d\n%d\n%d\n%d\n%d\n', l,m,n,o,p);
fclose(fid);
but the output,it has another extra data :
77,49,50
B2
4
3
0
10000
M12
77
49
50
3
1
1
1000
0
240
120
1520
0
0
why?
Walter Roberson
Walter Roberson on 31 Oct 2015
You print out f twice once as string and then as integer.

Sign in to comment.


dpb
dpb on 30 Oct 2015
From
help save
...
'-ascii' 8-digit ASCII format.
...
For ASCII file formats, the save function has the following
limitations:
* Each variable must be a two-dimensional double or char array.
* MATLAB translates characters to their corresponding internal
ASCII codes. For example, 'abc' appears in an ASCII file as:
9.7000000e+001 9.8000000e+001 9.9000000e+001
To write the data otherwise you'll have to use formatted with fprintf as the other higher-level routines such as csvwrite can only handle numeric arrays and you've got character data here.
What, specifically, are you trying to accomplish; writing the data to a file or saving it for use internally to the application? If the latter you can forget about the format and just use save/load and retrieve what was written transparently.

Tags

Community Treasure Hunt

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

Start Hunting!