The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: inclusion of alphanumeric string into numeric array MATLAB generated ASCII output
Replies: 1   Last Post: Jan 26, 2014 5:37 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 4
Registered: 7/17/13
inclusion of alphanumeric string into numeric array MATLAB generated ASCII output
Posted: Jan 25, 2014 8:30 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


In my *.m file I intend to write a CSV output ASCII file which contains previously generated numerical arrays, say COL-1 = time (or ?X?), and COL-2 = some previously calculated value, say acceleration signal. With attached macro <write_output.m> I managed to create an output, say <> that contains X,Y values, but I failed to include in the output file multiline header (containing customized file information, for example).

Specifically, my main *.m contains the following commands:

L1 = 'this is comment Line #1';
L2 = 'this is comment Line #2';
L3 = 'this is comment Line #3';
header = cellstr([L1; L2; L3]); %customizied lines of text to b e included in the OUT file.
Then I call file output macro:

output_fn = input('Type in OUT file-name (including extension):','s');
Where arrays {header}, {t} and {a} are previously defined. For example:

dim{t}=10, {t}=
dim{a}=10, {a}=

k = length(t) = length(t) = 10.

The above syntax leads to MLB termination error:

<Error using fprintf
Function is not defined for 'cell' inputs.>

...but changing the header definition into:

header = ([L1; L2; L3])

...didn?t help.

I?d appreciate if you can recommend appropriate syntax that would result with the desired output:
Say output_fn = ?MyOUT.txt?:

(START of ?MyOUT.txt?)
this is comment Line #1
this is comment Line #2
this is comment Line #3
-0.280000, -1.49624623e+00
-0.278000, -1.08843648e+00
-0.276000, -6.57560539e-01
-0.274000, -2.10040314e-01
-0.272000, +2.47351681e-01
-0.270000, +7.07592092e-01
-0.268000, +1.16351174e+00
-0.266000, +1.60790428e+00
-0.264000, +2.03363702e+00
-0.262000, +2.43376201e+00
(END of ?MyOUT.txt?)

Thanks in advance for the help!

Content of <write_output.m>:

%inputs: fn = defined in the calling *.m,
%use in main *.m, for example:
% fn = input('Type in OUT file-name (including extension):','s');
% write_output(output_fn,var1,var2,var3,dim);
%- - - - - - - - - - - - - - - - - - - - - - - - - -
function write_output2(fn,var1,var2,var3,dim);
fid = fopen(fn,'wt');
% detect multi-line header, if specified:
NHL = length(var1);
for ii = 1:NHL
% fprintf(fid,'%s'',var1(ii));
% fprintf(fid,'\n'); %CR
for ii = 1:dim
fprintf(fid,'%s ',',');
fprintf(fid,'%+15.8e',var3(ii)); %'+' = include sign
fprintf(fid,'\n'); %CR


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.