can anyone expalin num2str in the following script and what is the script doing

1 view (last 30 days)
count = 1;
j = 1;
%write the parameter section
while(count <= (max_xy-1))
st1 = ['110,' num2str(x(count),'%6.5f') ',' num2str(y(count),'%6.5f') ',' num2str(z(count),'%6.5f') ',' num2str(x(count+1),'%6.5f') ',' num2str(y(count+1),'%6.5f') ',' num2str(z(count+1),'%6.5f') ',' '0,0;'];
st1 = [st1 blanks(64-length(st1)) numstr(j,' ') numstr(count,'P')];
fprintf(fidw,[st1 '\n']);
count = count + 1;
j = j + 2;
end;
ip = max_xy-1;
Thanks in advance. numstr is a function
function st = numstr(k,ss)
temp_s = length(num2str(k));
tl = 8;
i = 1;
while(i <= (tl-temp_s-1))
st(i) = ' ';
i = i + 1;
end;
st = [ss st num2str(k)];
  1 Comment
Jan
Jan on 28 Feb 2012
Are you asking for "numstr" or "num2str"? To find out, what the one or the other function is doing, you can simply run them from the command line and see, how the outputs depend on the inputs.

Sign in to comment.

Answers (2)

dpb
dpb on 16 Jul 2014
Edited: dpb on 17 Jul 2014
numstr is inserting blanks length 8-length(theStringRepresentationOf the value k passed it). The loop could be replaced by a call to blanks and the function replaced by one of about three lines or so. The end result appears to be a fixed-length string from the concatenation of ss and the value of k with blanks inserted.
The function could also be replaced w/ a single sprintf call.
The whole thing is an extremely convoluted section of code writing a particularly-formatted .csv file. For example, the string st1 that is being built from a half-dozen individual terms can be replaced with
fmt=['110,' repmat('%6.5f,',1,6) '0.0'];
i1=count;i2=i1+1;
st1=sprintf(fmt,[x(i1:i2),y(i1:i2),z(i1:i2)].');
The concatenation of the subsequent pieces could also be replaced with proper formattting.
As Jan suggests, look at a sample of the output file created and just build the format strings required to create that output format. It should be only a couple of lines at most.
doc fprintf
and follow the links to the formatSpec info for the gory details.
The whole loop could be replaced by a judicious reshape and concatenation it would seem.

dpb
dpb on 19 Jul 2014
As mentioned above, the whole piece of code can be turned into a single output statement...
I created a sample set of data as
>> x=rand(10,1)*10; y=rand(10,1)*10; z=rand(10,1)*10;
and put the posted code in a file rsch.m as a function passed the three vectors and echoed the output to the screen getting...
>> rsch(x,y,z)
110,8.40717,7.51267,3.51660,2.54282,2.55095,8.30829,0,0; 1P 1
110,2.54282,2.55095,8.30829,8.14285,5.05957,5.85264,0,0; 3P 2
110,8.14285,5.05957,5.85264,2.43525,6.99077,5.49724,0,0; 5P 3
110,2.43525,6.99077,5.49724,9.29264,8.90903,9.17194,0,0; 7P 4
110,9.29264,8.90903,9.17194,3.49984,9.59291,2.85839,0,0; 9P 5
110,3.49984,9.59291,2.85839,1.96595,5.47216,7.57200,0,0; 11P 6
110,1.96595,5.47216,7.57200,2.51084,1.38624,7.53729,0,0; 13P 7
110,2.51084,1.38624,7.53729,6.16045,1.49294,3.80446,0,0; 15P 8
110,6.16045,1.49294,3.80446,4.73289,2.57508,5.67822,0,0; 17P 9
>> nr=size(x,1)-1;
>> fmt=['110,' repmat('%6.5f,',1,6) '0.0;%16dP%7d\n'];
>> fprintf(fmt,[x(1:nr) y(1:nr) z(1:nr) ...
x(2:end) y(2:end) z(2:end) ...
[1:2:2*nr].' [1:nr].'].')
110,8.40717,7.51267,3.51660,2.54282,2.55095,8.30829,0.0; 1P 1
110,2.54282,2.55095,8.30829,8.14285,5.05957,5.85264,0.0; 3P 2
110,8.14285,5.05957,5.85264,2.43525,6.99077,5.49724,0.0; 5P 3
110,2.43525,6.99077,5.49724,9.29264,8.90903,9.17194,0.0; 7P 4
110,9.29264,8.90903,9.17194,3.49984,9.59291,2.85839,0.0; 9P 5
110,3.49984,9.59291,2.85839,1.96595,5.47216,7.57200,0.0; 11P 6
110,1.96595,5.47216,7.57200,2.51084,1.38624,7.53729,0.0; 13P 7
110,2.51084,1.38624,7.53729,6.16045,1.49294,3.80446,0.0; 15P 8
110,6.16045,1.49294,3.80446,4.73289,2.57508,5.67822,0.0; 17P 9
And, lo! and behold, the results are the same...

Tags

Community Treasure Hunt

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

Start Hunting!