How can I upload 100 files with similar names and / or string in just one step in MATLAB?

I have 100 ASCII files in my directory, all are named as follows:

int_001.ASC
int_002.ASC
int_003.ASC
.
.
.
int_099.ASC
int_100.ASC

I need to import them into MATLAB with importdata, which should work as follows:

A = importdata('int_001.ASC', ' ', 9) x = A.data(:,1) y = A.data(:,2) 

My question is: how can I avoid writing 100 times importdata ? Is there a way to write only the first line and then load all the data?

thanks

+1
source share
3 answers
 fls = dir( 'int_*.ASC' ); for fi=1:numel(fls) A{fi} = importdata( fls(fi).name, ' ', 9 ); % ... end 

UPDATE:
You can use line formatting to read files according to their numbers:

 for fi=1:100 A{fi} = importdata( sprintf('int_%03d.ASC', fi ), ' ', 9 ); % ... end 
+10
source

You can use the strcat function in a for loop:

 for k=1:n fileName = strcat('int_',num2str(k, '%03d'),'.ASC'); A(k) = importdata(fileName, ' ', 9); x(k) = A(k).data(:,1); y(k) = A(k).data(:,2); end 
+4
source

If you want to do this a little overboard:

 alldata = arrayfun(... @(dirEntry)importdata(dirEntry.name, ' ', 9), ... dir('int_*.ASC'),... 'uniformoutput',false); 

This line executes the following

  • Gets a list of all files matching a partial file name as an array of structures (h / t Shai)
  • For each element in this array, an importdata call is importdata from your original message.
  • Compiles all outputs to an array of cells.
+2
source

Source: https://habr.com/ru/post/1484195/


All Articles