How Do You Upload a File to Matlab

MATLAB(TM) Hypertext Reference,  	          Copyright (c) 1995 Gerald Recktenwald,  	          All rights reserved

Loading Data into MATLAB for Plotting

In addition to plotting values created with its own commands, MATLAB is very useful for plotting information from other sources, e.g., experimental measurements. Typically this data is available as a plain text file organized into columns. MATLAB can easily handle tab or space-delimited text, but it cannot direct import files stored in the native (binary) format of other applications such every bit spreadsheets.

The simplest style to import your data into MATLAB is with the load command. Unfortunately, the load command requires that your data file contain no text headings or column labels. To get around this restriction you lot must utilize more avant-garde file I/O commands. Below I demonstrate both approaches with examples. I've included an m-file to handle the more complex case of a file with an arbitrary number of lines of text header, in improver to text labels for each column of data. Though hardly a cure-all, this function is much more than flexible than the load control because it allows y'all to provide documentation inside your data file.

Here'south an outline of this section

  • The MATLAB load Command
  • A simple plot of data from a file
  • Plotting information from files with column headings

The MATLAB load Control

There is more than one way to read information into MATLAB from a file. The simplest, though to the lowest degree flexible, process is to apply the load control to read the unabridged contents of the file in a unmarried step. The load command requires that the data in the file be organized into a rectangular array. No column titles are permitted. I useful class of the load control is

        load name.ext      

where ``proper name.ext'' is the name of the file containing the information. The result of this operation is that the information in ``name.ext'' is stored in the MATLAB matrix variable called proper name. The ``ext'' string is any three grapheme extension, typically ``dat''. Whatsoever extension except ``mat'' indicates to MATLAB that the data is stored as plain ASCII text. A ``mat'' extension is reserved for MATLAB matrix files (see ``help load'' for more information).

Suppose you had a uncomplicated ASCII file named my_xy.dat that independent two columns of numbers. The following MATLAB statements will load this data into the matrix ``my_xy'', and so re-create it into 2 vectors, x and y.

        >> load my_xy.dat;     %  read data into the my_xy matrix 	>> x = my_xy(:,1);     %  re-create first cavalcade of my_xy into x 	>> y = my_xy(:,2);     %  and 2d cavalcade into y      

You don't need to copy the information into x and y, of course. Whenever the ``10'' data is needed you could refer to it equally my_xy(:,1). Copying the information into ``ten'' and ``y'' makes the code easier to read, and is more than aesthetically highly-seasoned. The duplication of the data will non revenue enhancement MATLAB's memory for almost modest information sets.

The load control is demonstrated in the following example.

If the data you wish to load into MATLAB has heading data, e.yard., text labels for the columns, you have the following options to deal with the heading text.

  • Delete the heading information with a text editor and use the load command :-(
  • Use the fgetl command to read the heading information one line at at fourth dimension. You tin can and so parse the column labels with the strtok command. This technique requires MATLAB version 4.2c or later.
  • Use the fscanf command to read the heading information.

Of these options, using fgetl and strtok is probably the virtually robust and convenient. If yous read the heading text into MATLAB, i.east., if you don't use the load command, and so you volition have to also read the plot data with fscanf. The example, Plotting data from files with column headings shows how this is done.



A simple plot of data from a file

This example testify you how to load a uncomplicated data ready and plot information technology.

The PDXprecip.dat file contains two columns of numbers. The first is the number of the calendar month, and the 2nd is the mean precipitation recorded at the Portland International Airport between 1961 and 1990. (For an abundance of weather data like this check out the Oregon Climate Service)

Here are the MATLAB commands to create a symbol plot with the data from PDXprecip.dat. These commands are too in the script file precipPlot.m for you to download.

        >> load PDXprecip.dat;         %  read information into PDXprecip matrix  	>> month = PDXprecip(:,1);     %  re-create first column of PDXprecip into calendar month 	>> precip = PDXprecip(:,2);    %  and second column into precip  	>> plot(month,precip,'o');     %  plot precip vs. month with circles  	>> xlabel('month of the year');           % add axis labels and plot title 	>> ylabel('hateful atmospheric precipitation (inches)'); 	>> title('Hateful monthly atmospheric precipitation at Portland International Drome');      

Although the data in the calendar month vector is trivial, it is used here anyway for the purpose of exposition. The preceding statments create the following plot.

Plot of y = sin(3*pi*x)


Plotting data from files with cavalcade headings

If all your data is stored in files that comprise no text labels, the load control is all you need. I like labels, however, because they allow me to document and forget near the contents of a file. To use the load for such a file I would take to delete the carefully written comments everytime I wanted to brand a plot. And so, in society to minimize my effort, I might stop adding the comments to the data file in the showtime place. For us command freaks, that leads to an unacceptable increase in entropy of the universe! The solution is to find a way to have MATLAB read and deal with the text comments at the acme of the file.

The following example presents a MATLAB function that tin can read columns of data from a file when the file has an arbitrary length text header and text headings for each columns.

The information in the file PDXtemperature.dat is reproduced below

        Monthly averaged temperature (1961-1990) Portland International Aerodrome 	Source:  Dr. George Taylor, 	Oregon Climate Service, http://www.ocs.oregonstate.edu/ 	 	Temperatures are in degrees Farenheit 	 	Month   Loftier     Low    Average 	1	45.36	33.84	39.half-dozen 	2	50.87	35.98	43.43 	3	56.05	38.55	47.3 	4	60.49	41.36	50.92 	5	67.17	46.92	57.05 	six	73.82	52.8	63.31 	7	79.72	56.43	68.07 	eight	80.14	56.79	68.47 	9	74.54	51.83	63.eighteen 	10	64.08	44.95	54.52 	11	52.66	39.54	46.1 	12	45.59	34.75	xl.17      

The file has a 5 line header (including blank lines) and each column of numbers has a text label. To apply this information with the load control yous would have to delete the text labels and salvage the file. A ameliorate solution is to take MATLAB read the file without destroying the labels. Better nonetheless, we should be able to tell MATLAB to read and apply the column headings when it creates the plot fable.

At that place is no built-in MATLAB command to read this data, so we have to write an m-file to do the job. 1 solution is the file readColData.m. The full text of that function won't be reproduced here. Y'all tin can click on the link to examine the code and save it on your computer if you like.

Here is the prologue to readColData.one thousand

        part  [labels,ten,y] = readColData(fname,ncols,nhead,nlrows) 	%  readColData  reads data from a file containing data in columns 	%               that have text titles, and possibly other header text 	% 	%  Synopsis: 	%     [labels,10,y] = readColData(fname) 	%     [labels,x,y] = readColData(fname,ncols) 	%     [labels,x,y] = readColData(fname,ncols,nhead) 	%     [labels,ten,y] = readColData(fname,ncols,nhead,nlrows) 	%    	%  Input: 	%     fname  = proper name of the file containing the data (required) 	%     ncols  = number of columns in the information file.  Default = ii.  A value 	%              of ncols is required only if nlrows is also specified. 	%     nhead  = number of lines of header information at the very summit of 	%              the file.  Header text is read and discarded.  Default = 0. 	%              A value of nhead is required simply if nlrows is besides specified. 	%     nlrows = number of rows of labels.  Default = 1 	% 	%  Output: 	%     labels  =  matrix of labels.  Each row of lables is a different 	%                label from the columns of data.  The number of columns 	%                in the labels matrix equals the length of the longest 	%                column heading in the data file.  More than ane row of 	%                labels is immune.  In this case the 2d row of column 	%                headings begins in row ncol+1 of labels.  The third row 	%                cavalcade headings begins in row ii*ncol+1 of labels, etc. 	% 	%          Note:  Individual column headings must not contain blanks 	% 	%     x = column vector of x values 	%     y = matrix of y values.  y has length(x) rows and ncols columns      

The commencement line of the file is the function definition. Post-obit that are several lines of annotate statements that form a prologue to the function. Because the starting time line after the function definition has a non-blank annotate argument, typing ``help readColData'' at the MATLAB prompt volition crusade MATLAB to impress the prologue in the control window. This is how the on-line help to all MATLAB functions is provided.

The prologue is organized into four sections. Beginning is a cursory statement of what the role does. Side by side is a synopsis of the ways in which the function can exist called. Following that the input and output parameters are described.

Here are the MATLAB commands that apply readColData.m to plot the data in PDXtemperature.dat. The commands are likewise in the script file multicolPlot.m

        >>  %  read labels and x-y data 	>>  [labels,month,t] = readColData('PDXtemperature.dat',four,5);  	>>  plot(month,t(:,ane),'ro',month,t(:,two),'c+',month,t(:,3),'g-');  	>>  xlabel(labels(1,:));                      % add axis labels and plot title 	>>  ylabel('temperature (degrees F)'); 	>>  championship('Monthly average temperature for Portland International Drome');  	>>  %  add a plot legend using labels read from the file 	>>  legend(labels(ii,:),labels(3,:),labels(4,:));      

These statments create the post-obit plot.

Plot of High, Low and Average Temperatures



[Preceding Section] [Chief Outline] [Section Outline] [Next Section]

bowlingtheack.blogspot.com

Source: https://web.cecs.pdx.edu/~gerry/MATLAB/plotting/loadingPlotData.html

0 Response to "How Do You Upload a File to Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel