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 个评论
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
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))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!