Load multiple files one by one into a subfolder or Extract data from strut

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 commentaires

Dyuman Joshi
Dyuman Joshi le 19 Oct 2023
Modifié(e) : Dyuman Joshi le 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?
Stephen23
Stephen23 le 19 Oct 2023
Modifié(e) : Stephen23 le 19 Oct 2023
"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
University le 19 Oct 2023
Modifié(e) : University le 19 Oct 2023
Thank you Joshi for your response. Each of the files are tables that have several variables @ Joshi
@Stephen, what do you mean?
Hi Stephen... See the attached screenshoot
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.

Connectez-vous pour commenter.

 Réponse acceptée

matpath = 'C:..\..sol_file\';
matfiles = dir(fullfile(matpath ,'*.mat'));
full_file_names = fullfile(matpath,{matfiles.name});
for n = 1:numel(matfiles)
matfiles(n).data = load(full_file_names{n});
end
Now matfiles(1).data contains the data loaded from the 1st mat-file, matfiles(2).data contains the data loaded from the 2nd mat-file, and so on.

6 commentaires

Hi, thank you for your response. I tried this and is not different from what I had.
How is the result you get with this code different from the result you want?
Thank you Voss. I have been able to do it with your suggestion.
Please, how do i use strings say '0.001', '0.1', '1', '10' ... as index. For instance,
for i=1:length(nn)
xx = x(i)
end
I want the xx to have index of string, say xx_001, xx_01 etc?
"how do i use strings say '0.001', '0.1', '1', '10' ... as index"
You cannot do that. In MATLAB, an index is a positive integer (in general, an array of positive integers or an array of logicals).
Do you want to automatically create variables called xx_001, xx_01, etc.?
You shouldn't do that. See here:
In the example you give:
for i=1:length(nn)
xx = x(i)
end
why not just use x (i.e., if you want the first element of x, use x(1), if you want the second element of x, use x(2), and so on)?
Exactly, I want variable xx_001, xx_01, etc.
format long g
val = 0.001
val =
0.001
fprintf('%.999g\n', val)
0.001000000000000000020816681711721685132943093776702880859375
val2 = val * (1-eps)
val2 =
0.001
fprintf('%.999g\n', val2)
0.00099999999999999980397624721462079833145253360271453857421875
MATLAB does not store numeric values in decimal. The closest representable value to 0.001 is the one shown above. Why should it create a variable name xx_001 instead of xx followed by that string of digits?
Notice that val2 displays the same as val, but has a slightly different internal value and is not the closest representable value to 1/1000 (it is the second-closest) . If that were the value in the variable, then should xx_001 still be the created variable or should it be xx followed by that long string of digits for val2 ? What is the rule here for how much values should be rounded off in creating the name of the variable?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by