How to find text in csv file and filter information the table

17 次查看(过去 30 天)
Hello,
I am in despair about this problem in Matlab I am not progressing in. I am trying to find text in my csv file (I am sorry I cannot post anything from the file but its confidential) and after finding this text, I am trying to filter something from the numbers below it. The numbers can be something like: 0-15, with many digits after the comma. Here is some code I tried myself on:
L = uigetdir('\winfs\EA-proj');
% Identify where to search for files
% Store the name of all .xls files as a vector D
% D = dir([Location, '\*.csv']);
% Extract the file names
filenames = {L(:).name}.';
data = cell(length(L),1);
for ii = length(L):-1:1
% Create the full file name and partial filename
fullname = [Location filesep L(ii).name];
% Read in the data
data{ii} = xlsread(fullname);
end
[~,~,rawData] = xlsread(fullname);
name = 't_kammer';
% look at all row elements in the first column returning
% the first match only
idx = find(strcmp(rawData(:,1),name),1);
if ~isempty(idx)
total = sum(cell2mat(rawData(idx,2:end)));
else
total = 0;
end
If needed, I am working in the 2018R Version
  2 个评论
Andrew Janke
Andrew Janke 2020-1-31
How many digits is "many"? double values are good for about 16 decimal digits of precision.
And what exactly isn't working? You've described what you're doing, but not what you expect to happen and how your actual results are differing.

请先登录,再进行评论。

回答(1 个)

Cris LaPierre
Cris LaPierre 2020-1-31
I'll second Meg Noah. Don't use xlsread on a csv file. Try readtable.
We don't have enough info really help you code anything. Look into how to access data in a table. You may want to consider using the first column of data as rownames in your table using the ReadRowNames name-value pair.
  1 个评论
Black4Ghost
Black4Ghost 2020-2-3
Here is the working code I created if anybody needs it. It is only a fragment and not everything but its the part I asked for in this question.
function checkbox1_Callback()
h_push1 = findobj( 'Style', 'pushbutton',...
'String', 'Ordner Auswählen', 'Tag', 'pushbutton1' );
userdata_push = get( h_push1, 'UserData' );
filenames = {userdata_push.filelist(:).name};
flagInRange = false( 1, length( filenames ) );
for ii = length( filenames ):-1:1
fullname = [userdata_push.directory filesep userdata_push.filelist(ii).name];
[num, txt] = xlsread( fullname );
header = txt( 1,: );
header_flag = strcmp( header, 'Searched for String' );
counter = 1:1:length( header_flag );
counter = counter .* header_flag;
column_index = sum( counter );
column_number = num( :, column_index );
lower_limit = ( column_number >= 'Number you searching for' );
upper_limit = ( column_number <= 'Number you searching for' );
check_limit = double( lower_limit ) + double(upper_limit);
check_limit = (check_limit == 2);
if sum( check_limit ) > 0
flagInRange(ii) = true;
end
end
set( gcbo, 'UserData', flagInRange );
disp('Searching completed');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by