print inputted data in two aligned columns in matlab

You can use the "fprintf" function in MATLAB to print the inputted data in two aligned columns. Here's how you can do it:

main.m
% Get the input data
data = input("Enter data: ");

% Determine the number of rows and columns in the input data
[rows, cols] = size(data);

% Set the column width and precision
width = 10;
precision = 2;

% Print the data in two aligned columns
for i = 1:rows
    fprintf("%*.*f %*.*f\n", width, precision, data(i, 1), width, precision, data(i, 2));
end
355 chars
15 lines

In this code, we first get the input data using the "input" function. We then determine the number of rows and columns in the input data using the "size" function.

Next, we set the column width and precision for the columns using the "width" and "precision" variables.

Finally, we use a "for" loop to print the data in two aligned columns using the "fprintf" function. The "." notation in the "fprintf" format string tells MATLAB to use the "width" and "precision" variables for the column width and precision, respectively. The "%f" tells MATLAB to format the data as a floating-point number.

By using this code, you should be able to print the inputted data in two aligned columns in MATLAB.

gistlibby LogSnag