MATLAB Answers

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

34 views (last 30 days)
Black4Ghost
Black4Ghost on 12 Jan 2020
Commented: Black4Ghost on 3 Feb 2020
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 Comments

Andrew Janke
Andrew Janke on 31 Jan 2020
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.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 31 Jan 2020
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 Comment

Black4Ghost
Black4Ghost on 3 Feb 2020
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');

Sign in to comment.

Sign in to answer this question.


Translated by