print in two aligned columns in matlab

To print in two aligned columns in Matlab, you can use the fprintf function with a formatSpec that specifies the format of the output. Here's an example:

main.m
data = [1, 2, 3, 4, 5, 6];
formatSpec = '%-10d%-10d\n';
fprintf(formatSpec, reshape(data, 2, []));
99 chars
4 lines

In the above example, data is a row vector of numbers. The reshape function is used to reshape the data into a two-row matrix. The formatSpec string specifies that each element should take up 10 spaces, left-aligned with -, and there should be a newline \n after every two elements. The fprintf function then prints the formatted output.

You can adjust the width of the columns and the number of elements in each column as needed by changing the formatSpec string and the reshape function.

gistlibby LogSnag