How to iterate over data sets?

12 ビュー (過去 30 日間)
Daniela
Daniela 2024 年 6 月 17 日
編集済み: Star Strider 2024 年 6 月 17 日
Hi everyone,
I am new to coding so please bare with me as I am trying my best.
I am trying to plot and iterate over different data sets, for example, I want to plot data points 1-10, skip over 11 and 12, and then plot the next 10, 13-23 and so on and so forth. This is what I have so far. Please let me know where I have gone wrong. Thank you.
filename = "Mydatasheet.csv";
opts = detectImportOptions('filepath\Mydatasheet.csv','.csv');
T = readtable("Mydatasheet.csv",opts);
dummy1 = table2array(:,1);
dummy2 = table2array(:,2);
for ii= 1: length(dummy,2)
plot(dummy1(1:10:230),dummy2(1:10:230));
end

回答 (2 件)

Star Strider
Star Strider 2024 年 6 月 17 日
編集済み: Star Strider 2024 年 6 月 17 日
If you have the Signal Processing Toolbox, use the buffer function for this.
dummy1 = (1:60).'
dummy1 = 60x1
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
bfr1 = buffer(dummy1, 12);
disp(bfr1)
1 13 25 37 49 2 14 26 38 50 3 15 27 39 51 4 16 28 40 52 5 17 29 41 53 6 18 30 42 54 7 19 31 43 55 8 20 32 44 56 9 21 33 45 57 10 22 34 46 58 11 23 35 47 59 12 24 36 48 60
dummy1_out = bfr1(1:10,:)
dummy1_out = 10x5
1 13 25 37 49 2 14 26 38 50 3 15 27 39 51 4 16 28 40 52 5 17 29 41 53 6 18 30 42 54 7 19 31 43 55 8 20 32 44 56 9 21 33 45 57 10 22 34 46 58
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Otherwise, you can creatre your own limited version of it, for example —
bfr2 = bufr(dummy1, 12);
disp(bfr2)
1 13 25 37 49 2 14 26 38 50 3 15 27 39 51 4 16 28 40 52 5 17 29 41 53 6 18 30 42 54 7 19 31 43 55 8 20 32 44 56 9 21 33 45 57 10 22 34 46 58 11 23 35 47 59 12 24 36 48 60
dummy1_out = bfr2(1:10,:)
dummy1_out = 10x5
1 13 25 37 49 2 14 26 38 50 3 15 27 39 51 4 16 28 40 52 5 17 29 41 53 6 18 30 42 54 7 19 31 43 55 8 20 32 44 56 9 21 33 45 57 10 22 34 46 58
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function M = bufr(s,r)
% Star Strider's Emulation Of The Basic 'buffer' Function
% s = Signal Vector
% r = Desired number of rows
M = zeros(r,ceil(numel(s)/r));
M(1:numel(s)) = s;
end
EDIT — Corrected typographical errors.
EDIT — (17 Jun 2024 at 21:10)
Use the reshape functon to restore the edited ‘dummy1_out’ matrix to a column vector afterwards, so —
dummy1_out = reshape(dummy1_out, [],1);
disp(dummy1_out)
1 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 21 22 25 26 27 28 29 30 31 32 33 34 37 38 39 40 41 42 43 44 45 46 49 50 51 52 53 54 55 56 57 58
.

Hassaan
Hassaan 2024 年 6 月 17 日
編集済み: Hassaan 2024 年 6 月 17 日
An initial idea adjust as per your data format and requirements.
Better to share your .csv file.
% Load data from the CSV file
filename = "Mydatasheet.csv";
opts = detectImportOptions(filename); % Corrected path
T = readtable(filename, opts);
% Convert table to array
dataArray = table2array(T);
% Separate columns for X and Y data
dummy1 = dataArray(:, 1); % Assuming first column is X data
dummy2 = dataArray(:, 2); % Assuming second column is Y data
% Initialize figure
figure;
hold on; % Hold on to plot multiple segments on the same figure
% Iterate over the data in chunks
chunkSize = 10; % Number of points in each segment
skipIndices = [11, 12]; % Indices to skip
for startIdx = 1:chunkSize+length(skipIndices):length(dummy1)
endIdx = startIdx + chunkSize - 1;
% Ensure we don't exceed the array bounds
if endIdx > length(dummy1)
endIdx = length(dummy1);
end
% Plot the current segment
plot(dummy1(startIdx:endIdx), dummy2(startIdx:endIdx), 'DisplayName', ['Segment ' num2str(startIdx)]);
end
% Add labels and legend
xlabel('X Data');
ylabel('Y Data');
title('Plot of Data Segments');
legend show;
hold off; % Release the hold

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by