When I give a MATLAB project, I ask my students that the command window can only show output from disp and frprintf statements. All other statement outputs need to be suppressed. This is done to get an aesthetically pleasing report. So what is a student supposed to do when you may have y vs x data and one needs to show it as an ouput, especially when the number of y vs x data points can change.

For example we may have

x=[1 4 5 7 ]

y=[12 23 34 100]

How do I display this with just disp and fprintf statements?

- The MATLAB program link is here.
- The HTML version of the MATLAB program is here.
- DO NOT COPY AND PASTE THE PROGRAM BELOW BECAUSE THE SINGLE QUOTES DO NOT TRANSLATE TO THE CORRECT SINGLE QUOTES IN MATLAB EDITOR. DOWNLOAD THE MATLAB PROGRAM INSTEAD

%% HOW DO I DO THAT IN MATLAB SERIES?

% In this series, I am answering questions that students have asked

% me about MATLAB. Most of the questions relate to a mathematical

% procedure.

%% TOPIC

% How do I display arrays in MATLAB?

%% SUMMARY

% Language : Matlab 2008a;

% Authors : Autar Kaw and Jeremy Baker;

% Mfile available at

% http://numericalmethods.eng.usf.edu/blog/displaying_arrays.m;

% Last Revised : July 8, 2009;

% Abstract: This program shows you how to display arrays in MATLAB

clc

clear all

%% INTRODUCTION

disp(‘ABSTRACT’)

disp(‘ This program shows you how to display arrays’)

disp(‘ ‘)

disp(‘AUTHOR’)

disp(‘ Autar K Kaw of https://autarkaw.wordpress.com and Jeremy Baker’)

disp(‘ ‘)

disp(‘MFILE SOURCE’)

disp(‘ http://numericalmethods.eng.usf.edu/blog/displaying_arrays.m’)

disp(‘ ‘)

disp(‘LAST REVISED’)

disp(‘ July 8, 2009’)

disp(‘ ‘)

%% INPUTS

% Input x and y arrays, for example as y vs x data

x=[1 4 5 7 ];

y=[12 23 34 100];

%% DISPLAYING INPUTS METHOD 1

disp(‘INPUTS – Method 1’)

n=length(x);

fprintf(‘The number of data points is %g \n’,n)

disp(‘________________________’)

disp(‘ x-values y values ‘)

disp(‘________________________’)

for i=1:1:n

fprintf(‘ %g %g \n’,x(i), y(i)’)

end

disp(‘________________________’)

%% DISPLAYING INPUTS METHOD 2

% Suggested by guest blogger Jeremy Baker

disp(‘ ‘)

disp(‘INPUTS – Method 2’)

n=length(x);

fprintf(‘The number of data points is %g \n’,n)

disp(‘________________________’)

disp(‘ x y ‘)

disp(‘________________________’)

dataval=[x;y]’;

disp(dataval)

disp(‘________________________’)

_____________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.