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.
25 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Erwin Arias Hervert
el 13 de Dic. de 2022
Comentada: Erwin Arias Hervert
el 14 de Dic. de 2022
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
0 comentarios
Respuesta aceptada
millercommamatt
el 13 de Dic. de 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
Más respuestas (1)
Les Beckham
el 13 de Dic. de 2022
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1230202/220812_WT_MO_H134R_EA_1391%20015.txt')
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)'
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!