How to insert file name into an array?

8 views (last 30 days)
pink flower
pink flower on 25 Aug 2020
Answered: dpb on 25 Aug 2020
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
...

Answers (1)

dpb
dpb on 25 Aug 2020
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

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!