Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Create table

  1. Mar 10, 2010 #1
    Hi all,

    I'm writing a program for a graduate level course that I'm taking wherein I'm solving the Newtonian cooling solution for a long metal rod with specified properties.

    I have no problem setting up the code and generating a plot, but I am running into problems creating a table of the results. The requirement for the assignment is to present the data in both tabular and graphical format.

    If I'm dealing with, say, T(t) and t as dependent and independent variables, respectively, how do I go about creating a table, much like I could get from excel?

    For example, I'm hoping for something such as:

    T(t) t
    50 1
    40 2
    30 3

    Thanks in advance for your help.
  2. jcsd
  3. Mar 12, 2010 #2


    Staff: Mentor

    Your variables for T and t are array variables. Displaying them is a matter of looping through the pairs of values with a for loop, and displaying them with the fprintf function. Assuming your arrays have 50 elements, your code would look something like this.

    Code (Text):

    % Print a header to standard output device
    fprintf(1, "Temperature\t time\n")
    % Loop through temperature and time arrays
    for i = 1:50
       fprintf(1, "%d\t%d\n", T(i), t(i))
    The example data you showed consisted of integers, and I used conversion specifiers in the second fprintf call to accommodate integers. If your data is floating point, you will need different conversion specifiers. Here's a link to the fprintf documentation: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html.
  4. Jul 11, 2011 #3

    I am trying to write MATLAB codes for an excel table (Bsically create a function). In the first column (header), there are some characteristics and in the first row, there are some experimental results for the characteristics. How can I do that to address each datium in a specific row and column?

  5. Jul 11, 2011 #4


    User Avatar
    Science Advisor

    Welcome to PhysicsForums!

    In the future, if you have a new topic, please post a new topic, rather than posting in someone else's post, especially one that's over a year old.

    As for your specific question, are you asking how to import .xls (or .xlsx) files into MATLAB, or how to work with them once imported? If you're new to MATLAB, I highly suggest their Getting Started series, or their Tutorial series (e-mail registration is required for the Tutorial series, but not the Getting Started).


    Getting Started:
    Last edited by a moderator: Apr 26, 2017
  6. Jul 11, 2011 #5
    Thanks for your prompt response and sorry about replying here. I was wondering how I can import the excel file (the table) into the MATLAB?? Basically, I want to create a function to address each column and row.

    Thanks again
  7. Jul 11, 2011 #6


    User Avatar
    Science Advisor

    Well, there are two ways of importing Excel data, without having to manually do things line by line or column by column. The first is via the data importer, which has a nice graphical user interface--GUI--to guide you through the process (type in uiimport at the MATLAB prompt):

    The second is to use xlsread:
    http://www.mathworks.com/help/techdoc/ref/f16-5702.html#f16-14631 [Broken]

    You can find detailed information (including examples) on all MATLAB functions at the site I linked to. You can also find some basic information within the program by typing in help <NAME OF FUNCTION>. For instance, typing in the following will bring up a little blurb on the uiimport function:
    >> help uiimport

    Once you have it in MATLAB, you can address individual elements using the techniques here:

    Still, the MATLAB tutorials / getting started guide I linked to in my previous post will get you up to speed fairly quickly if you're new to MATLAB.
    Last edited by a moderator: May 5, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook