Using MATLAB with Data Saved from the Oscilloscope

 

      First you must save your data in a text file of extension filename.csv or filename.txt (if you don't have this yet, refer to the instructions for saving data from the oscilloscope).

      The next step is to edit your data. You must make sure that the file has no words or white space before the columns of data, and you often don't care about all the data you saved at once. Before running MATLAB, go in and edit your file to remove the data you don't care about (Always delete data in whole rows at once) and then save the file again under a new name, the .txt ending seems to work a little better for some reason. (Always keep a copy of your original file so no data is permanently lost.)

      Once the file contains only the information you care about, you are ready to work with it in MATLAB. Put your data file in the same directory as your MATLAB m-file and start MATLAB.

      In your MATLAB program, you will access your data with the following series of commands, where the variable names aren't important as long as you're consistent (don't forget the semicolons as there is lots of data here that will get printed if you don't):

% Open your file and stores the data in a matrix called variable1

variable1 = dlmread('filename.txt');

 

% Access the time variable (for example) found in the first column
% of data from the file.

time = variable1(: , 1);

% If we wanted the tenth row, weĠd use

% variable2 = variable1( 10 , : );

 

% The second column of the file represents the voltage measured on

% channel A1 of the oscilloscope.

voltage1 = variable1 (: , 2);

 

% You might think that the voltage measured on channel A2 of the

% 'scope would be saved in the third column, but actually it is

% saved in the fourth. If you look at your file, you'll

% notice that the first and third columns are identical--that is,

% they both represent time.

% Note: if you only read one source of data (A1 or A2) then you

% only have two columns, and no voltage2.

voltage2 = variable1 (: , 4);

 

% Often what you measured on the oscilloscope didn't start

% at time zero, so the following adjusts it to start at zero.

% This makes it match up with your calculated values, which

% presumably start at time zero.

adjustedTime = time - time(1);

 

% Now we just do regular plot commands to plot the calculated

% and measured data on the same plot.

% Assume that our calculated voltages (done in the program) are

% called V1 and V2, respectively, with calculated time T. Then

% the following command will plot the the calculated and measured

% values on the same scales.

plot (T, V1, adjustedTime, voltage1, T, V2, adjustedTime, voltage2)

 

% Now we add a legend to the plot.

legend('Calculated V1','Measured V1','Calculated V2','Measured V2')

 

% As a final note, you can dlmread 2 different files (which must

% be in the same directory as MATLAB is working from), just call

% them different names, access them with different variable names,

% and make sure you keep the times and voltages from each file

% together or you'll get errors from trying to plot matrices of

% different lengths.