I want to open a tab-delimited XY text file in matlab, use the numerical values of the XY columns to create a matrix and plot the signal.

33 vues (au cours des 30 derniers jours)
I have a tab-delimited text file with two columns: time and voltage. I'd like to open the file in matlab and put the data into a matrix (n x 2) to make an XY scatter plot. So far, I am able to open and read the file, create the matrix and initialize variables, but I'm struggling to find a way to put the data into the matrix.
Here is what I have:
% pointer to the file
fid = fopen("AxographSampleData\220812_WT_MO_H134R_EA_1391 015.txt","r");
% first line contains headers
headers = regexp( fgetl(fid) ,'\t','split');
timestamp = headers{1};
membrvolt = headers{2};
%% reads data from file and creates a matrix
% initialize matrix
nColumns = 2;
nRows = 40000;
data = zeros(nRows,nColumns);
% set a toggle switch and begin while loop
toggle = true;
while toggle
% get a line of tab-delimited data
aline = regexp( fgetl(fid), '\t','split');
% check whether we are at the end of data
if strcmpi(aline(1,2),'\s')
toggle = false;
else
% put the data point into the matrix at the appropiate position
end
end
I was following an example using a text file with six columns. What they did was to put the value of the 6th column in the matrix using columns 2 and 4 as indexes for data(rows,cols) with the following line of code:
% put the data point into thr matrix at the appropiate position
data(str2double(aline{2}), str2double(aline{4})) = str2double(aline{6});
But when I change the code to match the structure of my text file like this:
% put the data point into thr matrix at the appropiate position
data(str2double(aline{1})) = str2double(aline{2})
I get the following error message "Index in position 1 is invalid. Array indices must be positive integers or logical values".
Does anyone know how to fix this error? The text file is attached for reference.
Thanks in advance.
Erwin

Réponse acceptée

millercommamatt
millercommamatt le 13 Déc 2022
fid = fopen("AxographSampleData\220812_WT_MO_H134R_EA_1391 015.txt","r");
C = textscan(fid,'%f %f',"Delimiter",'\t','HeaderLines',1);
fclose(fid);
Sec_voltage_array = [C{1},C{2}];
% check out the textscan documentation in you want to import the seconds
% column as a duration and not a float

Plus de réponses (1)

Les Beckham
Les Beckham le 13 Déc 2022
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1230202/220812_WT_MO_H134R_EA_1391%20015.txt')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 40000×2 table
Time_s_ MembraneVoltage_1_V_ _______ ____________________ 5e-05 -0.075806 0.0001 -0.075819 0.00015 -0.075813 0.0002 -0.075806 0.00025 -0.0758 0.0003 -0.075813 0.00035 -0.075819 0.0004 -0.075806 0.00045 -0.075819 0.0005 -0.075825 0.00055 -0.075831 0.0006 -0.075838 0.00065 -0.075825 0.0007 -0.075806 0.00075 -0.075831 0.0008 -0.075825
scatter(T.Time_s_, T.MembraneVoltage_1_V_)
grid on
xlabel 'Time (s)'
ylabel 'Membrane Voltage 1 (V)'
In my opinion this looks better as a regular plot instead of a scatter plot.
figure
plot(T.Time_s_, T.MembraneVoltage_1_V_)
grid on
xlabel 'Time (s)'
ylabel 'Membrane Voltage 1 (V)'

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by