Problem extracting 2D array from multiple folders

1 ビュー (過去 30 日間)
Akeem Azeez
Akeem Azeez 2021 年 2 月 5 日
コメント済み: Akeem Azeez 2021 年 2 月 6 日
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 ( )
% 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
% 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)
listOfFolderNames = [listOfFolderNames singleSubFolder];
numberOfFolders = length(listOfFolderNames);
% 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;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
A_scan_Array_new(:,no_a_scans,image_number) = readmatrix(fullFileName)';
drawnow; % Force display to update immediately.
% Data_3D=A_scan_Array_new(:,:,1);
  4 件のコメント
Akeem Azeez
Akeem Azeez 2021 年 2 月 6 日
Thanks, I am new to this.



Cris LaPierre
Cris LaPierre 2021 年 2 月 6 日
編集済み: Cris LaPierre 2021 年 2 月 6 日
The only way I could get your code to run was to duplicate a portion of your dropbox folder structure (LV4Total/LV_4 and LV4Total/LV4) and then, when asked to confirm the top level folder, select LV4Total.
I then got the same error message you report. However, when I look at the displayed value of fullFileName, I can see that it is missing the last folder name.
Processing folder C:\Users\...\LV4Total\LV4
^^^ % file is in folder Lv4
Now reading .\LV4Total\000_100.dat
^ % Path is now missing the LV4 older
fullFileName = '.\LV4Total\000_100.dat'
^ % Path is now missing the LV4 older
Error using importdata (line 139)
Unable to open file.
I think you need to change what is currently line 51 to the following.
fullFileName = fullfile(thisFolder, baseFileName);
^^^^^^^^^^ % use thisFolder instead of myFolder
That at least gives a new error that you can work with.
Processing folder C:\Users\...\LV4Total\LV4
Now reading C:\Users\...\LV4Total\LV4\000_100.dat
^^^ % Path is now correct
fullFileName = 'C:\Users\...\LV4Total\LV4\000_100.dat'
^^^ % Path is now correct
Index exceeds the number of array elements (1).
Now just look at fixing your indexing (if needed). Again, I only pullied in one dat file. Your code is looking for 600, so if there are 600 in each folder, you should be ok.
  3 件のコメント
Akeem Azeez
Akeem Azeez 2021 年 2 月 6 日
Thank you very much!!!


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by