How to make a loop for importing files with different names

4 views (last 30 days)
I'm relatively junior when it comes to MatLab, this is the basic of what i'm trying to achieve: for
filename = 'G:\SCR\14_Delphi_SCR_test\Manufacturing Trials\B3.3 Armature trial\Acc.Test\0020\10v_100Hz_4.6ms.csv';
delimiter = ',';
startRow = 8;
...
% Allocate Variable Names
c0020_10v_100Hz = dataArray{:, 1};
a0020_10v_100Hz = dataArray{:, 2};
...
However i have other files that need to be imported as well they all have different names such as 11v_5Hz_4.4ms.csv or 16v_5Hz_3.6ms.csv
is there a way to create a loop that will import all the files as well as change the variable names so its c0020_filename and a0020_filename?
cheers

Answers (1)

dpb
dpb on 17 Dec 2014
Looks like for the given set of files that
d=dir('*5Hz*.csv');
for i=1:length(d)
x=importdata(d(i).name);
...
would find the files to open. Use an appropriate wildcard for your case as desired or, alternatively, return the listing of all *.csv files and insert a filter to nix the ones don't want as you process them or use more extensive pattern-matching on the names to select subset.
Another alternative yet is to use the file open gui and select files. See
doc uigetfile % for details on that route.
As for the second, yes, it's possible to create dynamic variables but "there be dragons". See the FAQ
for why and alternatives. Newer versions have additional discussion at
doc eval
For your case here, probably the dynamic field names for structures is a reasonable alternative although often it's better to simply leave the values in the array and use the column indices as the selector. One can name variables for these columns as mnemonic aids if desired.

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!