how to load files which are listed in a cell array

hello everyone,
I am doing aerodynamic computations. The blade i'm workin on is composed of several airfoils.
The airfoils data are contained into excel files. I have a total of 50 files.
However, i am trying to load each file for each section of the blade using for loop but i did not succeed.
I used the method given here:
Hence, i have the names of the files but i did not succeed to load each of the files.
Here the error i get:
>> names = dir('C:\........\BEMT_\airfoil_properties_*.xlsx');
>> filenames={names.name};
>> filenames(3)
ans =
'airfoil_properties_02.xlsx'
>> M=xlsread(filenames(3));
Error using xlsread (line 121)
File name must be a string.
Could you help me please ?
Best Regards

 Risposta accettata

Rik
Rik il 3 Mar 2023
Modificato: Rik il 3 Mar 2023
The filenames variable is a cell array, so you need {} to index the contents:
M=xlsread(filenames{3});
But it is better to use the struct returned by dir directly, as it allows you to include the path:
M=xlsread(fullfile(names(3).folder,names(3).name));

6 Commenti

Thank you for your response !
However, i am gettin the following error:
>> names = dir('C:\...\BEMT\airfoil_properties_*.xlsx');
>> filenames={names.name};
>> M=xlsread(filenames{3});
Error using xlsread (line 128)
XLSREAD unable to open file 'airfoil_properties_02.xlsx'.
File
'C:\...\BEMT\airfoil_properties_02.xlsx'
not found.
I think the error obtained is due to the fact that MATLAB searched for the desired excel file in the current directory. right ?
Is it possible to let MATLAB know the path of the filenames{3} ??
Best Regards
Sure, just use fullfile to compose the parts of the patch together. I'll edit my answer accordingly.
i am sorry but i think i did not understand what folder in names(3).folder refers to ?
Could you explain please ?
Best Regards,
Rik
Rik il 3 Mar 2023
Modificato: Rik il 3 Mar 2023
The variable names is a struct array that dir created. This code indexes the array and retrieves the value of the field. This field was introduced in the output several years ago (probably R2016b). Since you didn't mention you were using a release from 7 years ago (i.e. R2016a or older), it should have been safe to assume you were using a newer release.
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
nado
nado il 4 Mar 2023
Modificato: nado il 4 Mar 2023
the problem is that the MATLAB i'm usin is R2015a, so the field folder does not exist for the struct names.
>>> ver
----------------------------------------------------------------------------------------------------
MATLAB Version: 8.5.0.197613 (R2015a)
MATLAB License Number: $$$$$$$$$$$$$$$$$$$$$$$$$$$
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 17763)
Java Version: Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------
Thank you for your clarification and for suggesting to watch Onramp tutorial
Best Regards,
Just make sure to mention it next time (you can even put it in the release field when you ask a new question).
You need to use the same path you put in the dir call. I don't know a way to parse any wildcards for the folders.
M=xlsread(fullfile('C:\...\BEMT',names(3).name));

Accedi per commentare.

Più risposte (0)

Richiesto:

il 3 Mar 2023

Commentato:

Rik
il 4 Mar 2023

Community Treasure Hunt

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

Start Hunting!

Translated by