How do I display the data of an array in MATLAB?


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.

Advertisements

Published by

Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s