Looking for an efficient method to repeat the script command for multiple files
Show older comments
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
Adam
on 27 Jun 2018
What exactly is it that you are trying to do?
Rosalind Heron
on 27 Jun 2018
OCDER
on 27 Jun 2018
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?
Rosalind Heron
on 27 Jun 2018
OCDER
on 27 Jun 2018
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
Answers (3)
dpb
on 27 Jun 2018
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.
Jan
on 27 Jun 2018
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.
Rosalind Heron
on 5 Jul 2018
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!