MATLAB Answers

How to insert file name into an array?

조회 수: 2(최근 30일)
pink flower
pink flower 25 Aug 2020
답변: dpb 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
...

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(1개)

dpb
dpb 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

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by