extract certain Columns from table in which the value varies

4 次查看(过去 30 天)
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 个评论
Hussam Saddour
Hussam Saddour 2022-10-9
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 2022-10-9
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.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2022-10-9
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))

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by