Alfer
Alfer

Reputation: 31

How to write multiple variables using fprintf in short way in matlab?

my code is something like this

a= [2 4 5 8 6 7 88 9]
b= [12.8 41.3 13.7]
c= [16 18 20 10.1 17.5 49.5]
fprintf(out_file,'%d %d %d %d %d %d %d %d %f %f %f %d %d %d %f %f %f',a,b,c)

is it possible to write the conversions (%d....%f) in fprintf in a shorter form instead of repeating it so many times?

Or is there any other command that I can use to write the same into files?

Upvotes: 3

Views: 2209

Answers (4)

SecretAgentMan
SecretAgentMan

Reputation: 2854

@HansHirse's answer is excellent. Another alternative using repmat below. Could have compacted the code a bit but left in its current form for accessibility.

** Alternative Approach: ** repmat

a= [2 4 5 8 6 7 88 9];
b= [12.8 41.3 13.7];
c= [16 18 20 10.1 17.5 49.5];

fmtInt = '%d';    % format for integers
fmtFloat = '%f';  % format for floating point

fmtA = [repmat([fmtInt ' '],1,length(a)-1) fmtInt]
fmtB = [repmat([fmtFloat ' '],1,length(b)-1) fmtFloat]
fmtC = [repmat([fmtFloat ' '],1,length(c)-1) fmtFloat]

fmtstr = [fmtA ' ' fmtB ' ' fmtC]    % desired format string

% Can call fprintf() or sprintf() as required using format string

Upvotes: 1

rahnema1
rahnema1

Reputation: 15867

You can use cellfun:

cellfun                            ...
(                                  ...
    @(f,v) fprintf(outfile, f, v), ...
    {'%d ', '%f ', '%d ', '%f '},  ...
    {a, b, c(1:3), c(4:6)},        ...
    'UniformOutput' , false        ...
);

You can also use loop:

fmt = {'%d ', '%f ', '%d ', '%f '; a, b, c(1:3), c(4:6)};
for f = fmt;
    fprintf(outfile, f{:});
end

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112769

This answer assumes you don't need trailing decimal zeros. That is, [4 4.1] should be printed as 4 4.1, not as 4 4.10000 or 4.00000 4.10000.

Let

a = [2 4 5 8 6 7 88 9];
b = [12.8 41.3 13.7];
c = [16 18 20 10.1 17.5 49.123456789]; % example with 9 decimal figures
  • You can dynamically build the format string using repmat and strjoin. Also, you can use the format specifier %g, which automatically prints integer values without decimals:

    fprintf(strjoin([repmat({'%g'},1,numel(a)+numel(b)+numel(c))]),a,b,c)
    

    gives

    2 4 5 8 6 7 88 9 12.8 41.3 13.7 16 18 20 10.1 17.5 49.1235
    
  • If you need to specify a number of significant figures, avoiding trailing decimal zeros:

    fprintf(strjoin([repmat({'%.8g'},1,numel(a)+numel(b)+numel(c))]),a,b,c)
    

    gives

    2 4 5 8 6 7 88 9 12.8 41.3 13.7 16 18 20 10.1 17.5 49.123457
    
  • If you can do with a trailing space, you do not need to build the format string with repetition, as fprintf can automatically recycle it for all the inputs:

    fprintf('%.8g ',a,b,c)
    

    gives

    2 4 5 8 6 7 88 9 12.8 41.3 13.7 16 18 20 10.1 17.5 49.123457 
    

Upvotes: 5

HansHirse
HansHirse

Reputation: 18925

% Input.
a = [2 4 5 8 6 7 88 9];
b = [12.8 41.3 13.7];
c = [16 18 20 10.1 17.5 49.5];

% Concatenate inputs.
x = [a b c];

% Find integer values.
intidx = (floor(x) == x);

% Set format string.
formatstring = char((intidx.') * '%d ' + (not(intidx).') * '%f ');
formatstring = reshape(formatstring.', 1, numel(formatstring));

% Output.
sprintf(formatstring, x)

Upvotes: 3

Related Questions