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 CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by