Problem extracting 2D array from multiple folders
이전 댓글 표시
I want to write a code that will access a folder that contains 10 subfolders each containing 600 of .dat files. The .dat files contains 2500 elements. The aim is to build an array of 2500x600x10 to plot a 3-D image.
I have written a code that can access the subfolders and extract the .dat files from them but I have problem in getting the 2D array from each iteration of the loop. Here is my code and the link to the data ( https://www.dropbox.com/sh/tme38shk7q3reqm/AABiNghFVpZP4DS1da_8dP-7a?dl=0 )
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all .datin that folder and all of its subfolders.
clc; % Clear the command window.
clear all;close all;
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
myFolder= '/Users/.../Dropbox/LV4Total';
% Define a starting folder.
start_path = fullfile(myFolder);
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ':');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames);
no_a_scans=0;
image_number=1;
a_scans_per_image=600;
% Process all image files in those folders.k starts from 2 to exclude the base folder.
for k = 2: numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
filePattern = fullfile(thisFolder, '*.dat');
jpegFiles = dir(filePattern);
for p = 1:a_scans_per_image
baseFileName = jpegFiles(p).name;
no_a_scans=no_a_scans+1;
image_number=image_number+1;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
fullFileName
A_scan_Array_new(:,no_a_scans,image_number) = readmatrix(fullFileName)';
drawnow; % Force display to update immediately.
end
end
no_a_scans
% Data_3D=A_scan_Array_new(:,:,1);
댓글 수: 4
Ive J
2021년 2월 6일
And what error do you get?
Akeem Azeez
2021년 2월 6일
Cris LaPierre
2021년 2월 6일
Please don't change the question after it's been answered. It makes the post unintelligible for those who come across it later. I've added your new code to your corresponding reply below and reverted the code in the question back to what it was originally.
Akeem Azeez
2021년 2월 6일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!