Load multiple files one by one into a subfolder or Extract data from strut
Show older comments
Hi,
How can I load multiple files from a subfolder without merging them into a strut. ?
or
I have a .mat files in a folder I have loaded them using the command below.
matpath = 'C:..\..sol_file\';
matfiles = dir(fullfile(matpath ,'*.mat'));
Please how can I extract the files separately ?
6 Comments
Dyuman Joshi
on 19 Oct 2023
Edited: Dyuman Joshi
on 19 Oct 2023
The general consensus is to read them as a structure or a cell array and use indexing to access data - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
Why do you want to extract the files separately?
"I have a .mat files in a folder I have loaded them using the command below."
The code you show does not LOAD any file data into MATLAB memory.
"Load multiple files one by one into a subfolder..."
That does not make any sense: file data can be loaded into memory. Files are not loaded "into a subfolder". You could certainly copy or move files to a folder.
"...or Extract data from strut"
What is the expected output?
University
on 19 Oct 2023
Edited: University
on 19 Oct 2023
University
on 19 Oct 2023
University
on 19 Oct 2023
You can extract the imported file data into its own structure array, which will make the data easier to access. This assumes that each scalar structure in the DATA field has exactly the same fields (which so far you have not told us).
Given your structure S, after the file importing loop do this:
Z = [S.data];
After that you can trivially use indexing to access the data, e.g. for the 2nd file:
S(2).name % filename
Z(2).helloworld % the fields which so far you have not told us what they are called
Z(2).otherfields
Z(2).etcetcetc
Do NOT try to magically create all of that data as separate variables in the workspace. There be dragons.
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files 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!