ansam qas
ansam qas

Reputation: 1

using fprintf function in matlab to print 16 values per line from a variable matrix size

I have a loop that output variable number of values everytime, I want to use fprintf function to print these values so that each line contain 16 values only. I don't know the number of values since the loop outputs different number of values every time. any ideas to do this please?? thanks a lot

Upvotes: 0

Views: 5334

Answers (3)

Scott
Scott

Reputation: 1

If you are interested in intelligently creating a newline character at the end of each row (regardless of length), you can use the "\b" backspace character to remove lines at the end of a row, followed by a "\n" to make a new line. Example below:

fprintf('%u, %u \n',magic(3)) %will end the output with "2, "

fprintf('%u, %u \n',magic(4)) %will end the output with "1 {newline}"

In either case, sending 2 backspaces then a newline will result in a clean End Of Line:

fprintf('\b\b\n') % in one case, will truncate the ", " and in the other truncates " \n"

Upvotes: 0

Tik0
Tik0

Reputation: 2699

why not use an explicit counter for the fprintf-function:

printIdx = 1;    % Init the counter, so that after 16 iterations, there can be a linebreak
% Run a loop which just print the iteration index
for Idx = 42 : 100+randi(100,1);   % Run the loop untill a random number of iterations
    % ### Do something in your code ###
    fprintf('%d ',Idx); % Here we just print the index

    % If we made 16 iterations, we do a linebreak
    if(~mod(printIdx,16));
        fprintf('\n');
    end;
    printIdx = printIdx + 1;   % Increment the counter for the print
end

Upvotes: 0

horchler
horchler

Reputation: 18484

I don't know the datatype of your input variable or what type you want to output, so this is just an example:

a = ones(1,20); % 20 input values
fprintf('%d',a(1:min(numel(a),16)))

>> 1111111111111111

a = ones(1,10); % 10 input values
fprintf('%d',a(1:min(numel(a),16)))

>> 1111111111

The above prints at most 16 values and works even if the input, a is empty. The question is if you want a default value to be printed if there are fewer than 16 elements in your input. In that case, here's one way to do it:

a = ones(1,10); % 10 input values
default  = 0; % Default value if numel(a) < 16
fprintf('%d',[a(1:min(numel(a),16)) default(ones(1,max(16-numel(a),0)))])

>> 1111111111000000

You have to adjust these if you have a column vector.

EDIT:

To address a question raised by @Schorsch, if instead of clipping elements in arrays with greater than 16 values, you would like them to be printed on the next line, that can be done with this:

a = ones(1,20); % 20 input values
default = 0; % Default value if numel(a) < 16
fprintf('%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n',[a default(ones(1,16-mod(numel(a),16)))])

>> 1111111111111111
   1111000000000000

Variants of form can, of course, also be used in place of the first two solution I gave, but the print string can be harder to read.

Upvotes: 1

Related Questions