Looking for an efficient method to repeat the script command for multiple files

I seek a less time-consuming script function to repeat the process for multiple files in a folder. I have designed a script that can do this, but I believe there must be a more efficient method. Currently using:
for i=x1:x
name=sprintf('CSC%d.ncs',i);
initial='C:\Documents\MATLAB\Neuralynx\9Hz\';
test=strcat(initial,name);
[Timestamps{i}, Samples{i}, Header{i}]= Nlx2MatCSC(test, [1 0 0 0 1], 1, 1, []);
if i==1
save channel1 Timestamps* Samples* Header*
clear Header* Timestamps* Samples*
elseif i==2
save channel2 Timestamps* Samples* Header*
clear Header* Timestamps* Samples*
elseif i==3
save channel3 Timestamps* Samples* Header*
clear Header* Timestamps* Samples*
elseif i==4
save channel4 Timestamps* Samples* Header*
clear Header* Timestamps* Samples*
end
Useful suggestions greatly appreciated!!

5 Comments

What exactly is it that you are trying to do?
I am importing data from .ncs files to save them as .mat files.
The import of data requires the script mentioned here: [Timestamps{i}, Samples{i}, Header{i}]= Nlx2MatCSC(test, [1 0 0 0 1], 1, 1, []);
I have 32 files for each run, so I'm just looking for a straightforward command that can repeat this function for each file. Shown above is my current solution.
And why must you save them as .mat files? Is the Nlx2MatCSC function slow, so you want to save the output as a .mat file to resume for later use? But then, you're already saving the outputs into a cell array and there's no need to load the .mat file later. Is the 3 output cell arrays preallocated before the loop?
The .ncs files save the data in batches of 512, and use a .mex file to extract the data to Matlab. So I find that if I save them as .mat files I can restructure the data so it is not in batches of 512, and is therefore, useful for purpose. The 3 output cell arrays are preallocated in the script associated with the mex file before the loop. Yes.
Once you have the .mat files, how do you "restructure the data" from this .mat files? Can you show us that code too? Just trying to see if we can go from directly from load .ncs file to the joined data:
load ncs -> save mat -> load mat -> join data %your current strategy
load ncs -> join data %this would be nicer

Sign in to comment.

Answers (3)

folder='C:\Documents\MATLAB\Neuralynx\9Hz\'; % target subdirectory
file='CSC*.ncs'); % file suitable wildcard pattern
d=dir(fullfile(folder,file)); % get the dir() listing
for i=1:length(d) % process the files
[Timestamps{i}, Samples{i}, Header{i}]= Nlx2MatCSC(test, [1 0 0 0 1], 1, 1, []);
...
At this point need more detail on what the variables are and how they need to be processed.
In general, creating named sequential files/variables is NOT the way to proceed.
If, for example, the timestamps are the same and the data are simple values for each of the channels, it might make sense to build a timetable structure of them all.
It is a bold guess only:
initial='C:\Documents\MATLAB\Neuralynx\9Hz\';
for i = 1:x
name = sprintf('CSC%d.ncs',i);
test = fullfile(initial, name);
[Timestamps, Samples, Header]= Nlx2MatCSC(test, [1 0 0 0 1], 1, 1, []);
save(sprintf('channel%d', i), 'Timestamps', 'Samples', 'Header');
end
This will not be much faster, because the main work is spent in Nlx2MatCSC and in save.
For closure, I've ended up with the following script. It's not much faster, but it's at least neater. True, I don't need this for my analysis, but also my boss was having trouble viewing the raw files on their computer, so my solution was to save these as matlab files and share them that way.
for i=firstchannel:lastchannel
name=sprintf('CSC%d.ncs',i);
channel=strcat(name);
[Timestamps{i}, Samples{i}, Header{i}]= Nlx2MatCSC(channel, [1 0 0 0 1], 1, 1, []);
save (['channel' num2str(i) '.mat'] ,'Timestamps' ,'Samples', 'Header')
clear Header* Timestamps* Samples*
channelfile=sprintf('channel%d.mat',i);
load(channelfile)

Products

Release

R2017a

Asked:

on 27 Jun 2018

Answered:

on 5 Jul 2018

Community Treasure Hunt

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

Start Hunting!