Read non-negative cells from a cell array
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I have following code to read two csv files. I need to read the non-negative cell values from col 6 of Dt cell array and corresponding cell array from col 5 with YYYY-MM-DD format and store it in "Data" cell array.
Any suggestions to do that inside the for loop. Please see the two csv files attached.
Thanks in advance.
clear all
cd ('<path1>')
myFolder = '<path2>';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.csv');
csvFiles = dir(filePattern);
celsplt = @(x) strsplit(x, ',' ,'CollapseDelimiters',1);
for k = 1:length(csvFiles)
fid(k) = fopen(fullfile(myFolder,csvFiles(k).name));
out{k} = textscan(fid(k),'%s%s%f','delimiter','\t');
Ds = cellfun(celsplt,out{1,k}{1,1}, 'Uni',0);
Dt{k} = vertcat(Ds{:});
fclose(fid(k));
end
Sample of Dt cell array is shown below.
Dt=
'KH' '100401' 'PH' 'M' '1970-12-31T07:00:00+07:00' '-9999.00' 'mm' 'O'
'KH' '100401' 'PH' 'M' '1971-01-01T07:00:00+07:00' '0.00' 'mm' 'O'
'KH' '100401' 'PH' 'M' '1971-01-02T07:00:00+07:00' '0.00' 'mm' 'O'
回答 (1 件)
Voss
2024 年 1 月 9 日
myFolder = '.';
filePattern = fullfile(myFolder, '*.csv');
csvFiles = dir(filePattern);
N = numel(csvFiles);
Data = cell(N,1);
for k = 1:N
C = readcell(fullfile(myFolder,csvFiles(k).name));
Data{k} = C([C{:,6}]>=0,[5 6]); % use 5 instead of [5 6] if you just want the dates
end
disp(Data)
That makes Data a cell array with a cell for each file. If you want everything together you can do:
Data = vertcat(Data{:})
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!