How to read multiple files from one from folder /dir

조회 수: 27 (최근 30일)
Iyk
Iyk 2016년 6월 17일
편집: Stephen23 2016년 6월 21일
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

답변 (1개)

Stephen23
Stephen23 2016년 6월 17일
편집: Stephen23 2016년 6월 17일
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:
  1. 0 output when the user select no files.
  2. string output, when the user selects one file.
  3. cell array of strings output, when the users selects multiple files.
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
Iyk
Iyk 2016년 6월 21일
편집: Stephen23 2016년 6월 21일
Thanks Stephen Please I incorporated your code as shown below:
tmp = uigetfile('*.bin*','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);
for k = 1:paraC(1)
ymean = mean(yC(:,k));
yC(:,k) = yC(:,k)-ymean;
ytemp(:,k) = yC(:,k)-(k-1)*0;
end
y1=yC(:,2);
figure
plot(xC,ytemp)
xlabel('Time (s)')
ylabel('Amplitude (V)')
hold on
plot (xC,ytemp(:,2),'r')
signal=ytemp(:,2);
figure(1)
signalcut=signal(x>4.58e-04 & x< 7.12e-04);
figure(2)
plot(x(xC>4.58e-04 & xC< 7.12e-04),signalcut)
xlabel('Time (s)')
ylabel('Amplitude (V)')
RMS = sqrt(mean([ytemp(xC>4.58e-04 & xC< 7.12e-04)].^2))
end
and I got an error:
"Undefined function '_colonobj' for input
arguments of type 'cell'.
Error in multfil (line 14)
for k = 1:paraC(1)"
I have tried to look that up to see what it means but I could make sense of it. Can you please help
Stephen23
Stephen23 2016년 6월 21일
편집: Stephen23 2016년 6월 21일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by