Clear Filters
Clear Filters

extracting rows of data

28 views (last 30 days)
Sneha Kandapal
Sneha Kandapal on 20 Oct 2023
Commented: Star Strider on 23 Oct 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]);

Accepted Answer

Star Strider
Star Strider on 20 Oct 2023
Try this —
T1 = readtable('07010000002.txt');
T1.Properties.VariableNames = {'V','I'}
T1 = 25025×2 table
V I ________ _______ -0.19667 -3.0823 -0.19333 -3.0827 -0.19333 -3.0819 -0.2 -3.0837 -0.19667 -3.0819 -0.19667 -3.0819 -0.19667 -3.081 -0.2 -3.0825 -0.19667 -3.0817 -0.19667 -3.0815 -0.19667 -3.0829 -0.2 -3.0823 -0.19667 -3.0823 -0.2 -3.0827 -0.19667 -3.0823 -0.19667 -3.0821
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)
minidxv = 6×1
4502 6168 7834 9500 11167 12833
maxV = max(T1.V)*0.99;
maxidx = find((T1.V >= maxV) & (T1.I >= maxI));
stpmax = find(diff([0; maxidx]) > 100);
maxidxv = maxidx(stpmax)
maxidxv = 6×1
5496 7162 8827 10494 12160 13826
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
07010000002.txt VI_04502_05496.txt VI_06168_07162.txt VI_07834_08827.txt VI_09500_10494.txt VI_11167_12160.txt VI_12833_13826.txt
.
  2 Comments
Sneha Kandapal
Sneha Kandapal on 23 Oct 2023
Thank you this worked.
Star Strider
Star Strider on 23 Oct 2023
As always, my pleasure!

Sign in to comment.

More Answers (3)

dpb
dpb on 20 Oct 2023
Edited: dpb on 20 Oct 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))]
ans = 1×2
-1.2033 -1.2033
i1=find(data(:,1)==-1.2)
i1 = 0×1 empty double column vector
i2=find(data(:,1)== 1.2)
i2 = 0×1 empty double column vector
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)
ix1 = 10×1
4502 6168 7834 9500 11166 12833 14499 16164 17830 19496
[~,ix2]=findpeaks( data(:,1),'minPeakHeight',1.2-dV,'minPeakDistance',1000)
ix2 = 10×1
5498 7165 8829 10497 12163 13828 15495 17162 18828 20493
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)))

Voss
Voss on 20 Oct 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

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 20 Oct 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])

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!