extract certain Columns from table in which the value varies

2 views (last 30 days)
i have a 4 column table, i need to extract certain arrays based on the number of the 3rd column and plot x=column2 ,y=column4
note that the number of elements in each iteration is not constant (in this example 76) it may be more or less, and the Values in the 3rd column are also not fix
  3 Comments
Hussam Saddour
Hussam Saddour on 9 Oct 2022
i have attached a new table actually x,y refers to each element of the same value on the third column, so i have to create and plot arrays seperatly that have a common Value on 3rd column, hope that explained what i mean
dpb
dpb on 9 Oct 2022
We know what you meant; @Davide Masiello was suggesting using the very good MATLAB documentation that includes <read-a-sequence-of-spreadsheet-files> to give it a shot on your own first; "learning by doing" is typically much more effective than just having somebody hand off a complete solution.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 9 Oct 2022
Try this, modifying the file pattern as needed:
% Get a list of files
filePattern = fullfile(pwd, 'Microsoft Excel-Arbeitsblatt*.xlsx');
fileList = dir(filePattern);
numFiles = numel(fileList)
for fileNumber = 1 : numFiles
% Read in this particular file.
fullFileName = fullfile(fileList(fileNumber).folder, fileList(fileNumber).name);
fprintf('Reading in file #%d of %d.\n', fileNumber, numFiles);
data = readtable(fullFileName);
[rows, columns] = size(data);
% Initialize x and y for this file.
x = [];
y = [];
% Extract the third column and determine if it's x or y
for row = 1 : rows
if strcmpi(data{row, 3}, 'x')
x = [x; data{row, 2}];
elseif strcmpi(data{row, 3}, 'y')
y = [y; data{row, 2}];
end
end
% Plot it.
if ~isempty(x)
figure;
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('x');
ylabel('y')
title(fileList(fileNumber).name)
end
end
message = sprintf('Done processing %d files.', numFiles);
fprintf('%s\n', message);
uiwait(helpdlg(message))

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!