Importing Data in MATLAB: Plotting Time vs. Third Column

In summary, to load data from a .csv file, you would first manipulate the data and then use string operations to extract the necessary substrings. Once you have the data in the desired format, you can plot it using the appropriate code. There is no need to run textscan again.
  • #1
760
2
Hi. Suppose I load data from a .csv file:

Blah
@Blah
2Blah
Blah
f Blah
fa Blah
Blahasd
Blahasda
vasf Blah
as Blah
asdas Blah



Date Time Ch1:
07/25/2012 42:46.0 96.385
07/25/2012 42:46.0 -177.608
07/25/2012 42:46.0 155.481
07/25/2012 42:46.0 64.15

an attempt would be to manipulate the data first before I read it, things would be much more subtle:

fid = fopen('PULL1.CSV');

data = textscan(fid, '%s' ,'delimiter', '\b');

data2 = data{:,:}

data3 = data2(13:end,:);

fclose(fid);

-----

The output then is:

'07/25/2012,17:49:56.080,37.288'
'07/25/2012,17:49:56.085,37.288'
'07/25/2012,17:49:56.090,53.405'Would I still have to run textscan to recognize the entries separated by the commas?

How would I be able to plot time vs. the third column?
 
Physics news on Phys.org
  • #2
Nusc said:
Would I still have to run textscan to recognize the entries separated by the commas?
Once you have the data in data3, you don't have to run textscan again. Just use string operations to take out each substring.
Nusc said:
How would I be able to plot time vs. the third column?
The following code will work:
Matlab:
data3 = string(data3);
for i = 1:size(data3)
    index = strfind(data3(i), ',');
    date = extractBefore(data3(i), index(1));
    t = extractBetween(data3(i),index(1)+1,index(2)-1);
    val(i) = extractAfter(data3(i),index(2)+1);
    total = convertStringsToChars(strcat(date," ",t));
    time(i) = datetime(total, 'InputFormat', 'MM/dd/yyyy HH:mm:ss.SSS');
end
plot(time,val)
 

1. How do I import data into MATLAB from an external file?

To import data into MATLAB, you can use the importdata or readtable functions. These functions allow you to specify the file name, format, and other options for importing the data. Additionally, you can also use the Import Tool in the MATLAB Editor to visually select and import data from a file.

2. What is the best way to plot time vs. a third column of data in MATLAB?

The best way to plot time vs. a third column of data in MATLAB is to first import the data using one of the methods mentioned above. Then, use the plot or scatter function to plot the third column of data against the time values. You can also use the xlabel and ylabel functions to label the axes with appropriate names.

3. How do I label the time axis in a MATLAB plot?

To label the time axis in a MATLAB plot, you can use the xlabel function and specify the label name as a string. For example, if your time values represent hours, you can use xlabel('Time (hours)') to label the x-axis as "Time (hours)". You can also use the datetick function to customize the appearance of the time values on the axis.

4. Can I customize the appearance of the plot in MATLAB?

Yes, you can customize the appearance of the plot in MATLAB using various functions and options. For example, you can use the title function to add a title to the plot, the legend function to add a legend, and the grid function to display grid lines. You can also use the plot or scatter function with additional input arguments to change the color, style, and other properties of the plotted data.

5. How can I export a plot in MATLAB to an image file?

To export a plot in MATLAB to an image file, you can use the saveas function. This function allows you to specify the plot handle and the file format (e.g. PNG, JPEG, etc.) as input arguments. You can also use the print function to customize the resolution and other properties of the exported image file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top