extracting rows of data
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sneha Kandapal
el 20 de Oct. de 2023
Comentada: Star Strider
el 23 de Oct. de 2023
Hello friends,
I have a txt file with two columns X for voltage and Y for current. I have sweeped the voltage from -1.2 to 1.2 multiple times. I want to write a matlab code where I can seperate rows of data ranging from -1.2 to 1.2 and save it in txt file. That means if the code finds five set of -1.2 to 1.2 sweeps it should extract and create five different txt files.
I tried writing a code but it looks like it is not extracting the data but giving teh same input file.
% Prompt the user to select the input data file
[inputFileName, inputFilePath] = uigetfile('*.txt', 'Select the input data file');
if inputFileName == 0
% User canceled the file selection
disp('File selection canceled. Exiting.');
return;
end
% Load the data from the selected input file
inputFile = fullfile(inputFilePath, inputFileName);
data = load(inputFile);
% Define the range you want to extract
minVoltage = -1.2;
maxVoltage = 1.2;
% Find the indices within the specified range
indices = (data(:, 1) >= minVoltage) & (data(:, 1) <= maxVoltage);
% Extract data within the specified range
outputData = data(indices, :);
% Prompt the user to select where to save the extracted data
[saveFileName, saveFilePath] = uiputfile('*.txt', 'Select where to save the extracted data');
if saveFileName == 0
% User canceled the save file selection
disp('Save file selection canceled. Exiting.');
return;
end
% Create the full path for the output file
outputFile = fullfile(saveFilePath, saveFileName);
% Save the extracted data to the output file
writematrix(outputData, outputFile, 'Delimiter', 'tab');
disp(['Data extracted and saved successfully to ' outputFile]);
0 comentarios
Respuesta aceptada
Star Strider
el 20 de Oct. de 2023
Try this —
T1 = readtable('07010000002.txt');
T1.Properties.VariableNames = {'V','I'}
minI = min(T1.I)*0.99;
maxI = max(T1.I)*0.99;
minV = min(T1.V)*0.99;
minidx = find((T1.V <= minV) & (T1.I <= minI));
stpmin = find(diff([0; minidx]) > 100);
minidxv = minidx(stpmin)
maxV = max(T1.V)*0.99;
maxidx = find((T1.V >= maxV) & (T1.I >= maxI));
stpmax = find(diff([0; maxidx]) > 100);
maxidxv = maxidx(stpmax)
figure
plot(minidx)
hold on
plot(maxidx)
hold off
grid
figure
tiledlayout(3,2)
for k = 1:numel(minidxv)
nexttile
IV{k} = sortrows(T1(minidxv(k):maxidxv(k),:), 1);
plot(IV{k}.V, IV{k}.I)
grid
axis('padded')
xlabel('V')
ylabel('I')
filename = sprintf('VI_%05d_%05d.txt',minidxv(k),maxidxv(k));
title(strrep(filename,'_','\_'))
writetable(IV{k},filename)
end
dir('*.txt') % Show Files Have Been Written
.
2 comentarios
Más respuestas (3)
dpb
el 20 de Oct. de 2023
Editada: dpb
el 20 de Oct. de 2023
% % Find the indices within the specified range
% indices = (data(:, 1) >= minVoltage) & (data(:, 1) <= maxVoltage);
% % Extract data within the specified range
% outputData = data(indices, :);
The above will output all of the data within min/max over the entire file; it won't segregate separate sections of the sweep; to do that you would have to find separately the two end points and loop over those taking just the values between each pair.
d=dir('*.txt');
data=readmatrix(d.name);
[min(data(:,1)) min(data(:,1))]
i1=find(data(:,1)==-1.2)
i2=find(data(:,1)== 1.2)
assert(numel(i1)==numel(i2)) % make sure have matching pairs
Excepting, you don't have precisely 1.2V on either end so you'll have to be more flexible in finding starting points...
plot(data(:,1))
Oh. That's not what had imagined--each sweep returns to baseline between next...take a little different tack on finding the breakpoints then...
dV=0.025;
[~,ix1]=findpeaks(-data(:,1),'minPeakHeight',1.2-dV,'minPeakDistance',1000)
[~,ix2]=findpeaks( data(:,1),'minPeakHeight',1.2-dV,'minPeakDistance',1000)
assert(numel(ix1)==numel(ix2))
subplot(2,1,1)
plot(data(:,1))
hold on
for i=1:numel(ix1)
plot(ix1(i),data(ix1(i),1),'*')
plot(ix2(i),data(ix2(i),1),'x')
end
ylim([-1.25 1.25]), xlim([ix1(1)-250 ix2(end)+250])
subplot(2,1,2)
hold on
for i=1:numel(ix1)
plot(data(ix1(i):ix2(i),1),data(ix1(i):ix2(i),2))
end
legend(string(1:numel(ix1)))
0 comentarios
Voss
el 20 de Oct. de 2023
inputFile = '07010000002.txt';
data = readmatrix(inputFile);
figure
hold on
plot(data(:,1))
min_idx = find(islocalmin(data(:,1),'MinProminence',0.5,'MinSeparation',1000));
max_idx = find(islocalmax(data(:,1),'MinProminence',0.5,'MinSeparation',1000));
plot([min_idx;max_idx],data([min_idx;max_idx],1),'ro')
% create a separate text files for each sweep (from min_idx to max_idx) section:
for ii = 1:numel(min_idx)
data_section = data(min_idx(ii):max_idx(ii),:);
output_file = sprintf('section_%02d.txt',ii);
writematrix(data_section,output_file);
end
0 comentarios
Sulaymon Eshkabilov
el 20 de Oct. de 2023
The answer lies in your original data - see the plot of your data. You are extracting the same data with your logical indexing of indices = (data(:, 1) >= minVoltage) & (data(:, 1) <= maxVoltage); % where your actual data in column 1 for voltage varies only within [-1.2 to 1.2].
data = load('07010000002.txt');
plot(data(:,1), data(:,2), 'b-')
grid minor
xlabel('Voltage')
ylabel('Current')
xlim([-1.22 1.22])
0 comentarios
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!