- 0 output when the user select no files.
- string output, when the user selects one file.
- cell array of strings output, when the users selects multiple files.
How to read multiple files from one from folder /dir
27 views (last 30 days)
Show older comments
Hey Guy,
Please I am trying to write a code to run multiple (.bin) file from one folder. So far, This is what I have
file=transpose(dir('*.bin'));
for file=file
% [fname,pname] = uigetfile('*.bin','Input data file (*.bin)');
[filename,pathname] = uigetfile('*.bin','Select file(s)Input data file (*.bin)','MultiSelect','on')
disp( file.name )
FileName = (file.name);
[x,y,fs,para] = fnlvsgl(filename,0);
for i = 1:para(1)
ymean = mean(y(:,i));
y(:,i) = y(:,i)-ymean;
ytemp(:,i) = y(:,i)-(i-1)*0;
end
y1=y(:,2);
figure
plot(x,ytemp)
xlabel('Time (s)')
ylabel('Amplitude (V)')
hold on
plot (x,ytemp(:,2),'r')
signal=ytemp(:,2);
figure(2)
SC=signal(x>4.58e-04 & x< 7.12e-04);
figure(2)
plot(x(x>4.58e-04 & x< 7.12e-04),SC)
xlabel('Time (s)')
ylabel('Amplitude (V)')
%
% %saveas(gcf,'AJS.fig');
RMS = sqrt(mean([ytemp(x>4.58e-04 & x< 7.12e-04)].^2))
end
but it gives me an error such as
Error using fopen
First input must be a file name of type
char, or a file identifier of type double.
Error in fnlvsgl (line 30)
fid = fopen(fname,'r','b');
Error in trial4chika2 (line 10)
[x,y,fs,para] = fnlvsgl(filename,0);".
Can any one help?
I just need to be able to run all the files in my folder in one click.
Thanks
0 Comments
Answers (1)
Stephen23
on 17 Jun 2016
Edited: Stephen23
on 17 Jun 2016
As the uigetfile documentation clearly states, "If 'MultiSelect' is 'on' and you select more than one file in the dialog box, then FileName is a cell array of strings"
So when you select multiple files, your variable filename is a cell array of strings. And fnlvsgl (whatever this is) clearly only works with strings.
This means you need to write your code to consider three cases:
Something like this:
tmp = uigetfile('*.m','Select file','MultiSelect','on');
assert(ischar(tmp)||iscell(tmp),'no file selected') % 0 output
if ischar(tmp) % char output
tmp = cellstr(tmp);
end
% preallocate output cell arrays:
fsC = cell(size(tmp));
paraC = fsC; xC = fsC; yC = fsC;
% loop over file names:
for k = 1:numel(tmp)
disp(tmp{k})
[xC{k},yC{k},fsC{k},paraC{k}] = fnlvsgl(tmp{k},0);
end
Summary: read the documentation: it tells you what MATLAB functions do, and how to use them.
2 Comments
Stephen23
on 21 Jun 2016
Edited: Stephen23
on 21 Jun 2016
I agree that error message is pretty cryptic. It refers to the fact that paraC is a cell array, and so paraC(1) is the first cell of this cell array. If you want to get the content of the first cell, then you need to use cell indexing:
paraC{1}
Because of course the colon operator that you are using, 1:paraC(1), only works with a numeric value, not a cell, therefore it must be 1:paraC{1}. Learn about cell array indexing:
Note: your code formatting is a bit untidy: in the MATLAB editor select all of your code and clicking ctrl+i. Much tidier!
See Also
Categories
Find more on File Operations 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!