How to insert file name into an array?

7 ビュー (過去 30 日間)
pink flower
pink flower 2020 年 8 月 25 日
回答済み: dpb 2020 年 8 月 25 日
I have a sequence of 62496 files. Each file name has information about height, year, month, day, hour and minute, for example, 14000_20140615_03000.dat
My script looks like the following:
clear all;
close all;
str='../Documents/';
folder_name = uigetdir(str);
files = dir(fullfile(folder_name,'*.dat'));
curr_folder=pwd;
cd(folder_name);
for i = 1: length(files);
fileID = fopen(files(i).name);
var = fread(fileID,[500 500], 'float32');
var20 = find(var>=20);
end
I want to create a matrix with the index of var20 and the height of this data, which is the information in the file name. That is, I need to obtain a matrix with two columns like this:
18582 14000
19647 14000
15824 14000
...

回答 (1 件)

dpb
dpb 2020 年 8 月 25 日
str='../Documents/';
folder_name = uigetdir(str);
d=dir(fullfile(folder_name,'*.dat'));
for i = 1: length(files);
fid=fopen(fullfile(d(i).folder,d(i).name);
var = fread(fileID,[500 500], 'float32');
fid=fclose(fid);
var20 = find(var>=20);
ht=str2double(extractBefore(d(i).name,'_')); % get the height number from filename
heightArray=[repmat(ht,numel(var20),1) var20]; % create the new array
% either use these data here or save to new file or whatever before going on to next...
...
end

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by