Error when importing with csvread
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%% Import data
numfiles = 12; % number of excel files
data = cell(numfiles,1);
data1 = data(1:end,:);
for i=10:numfiles; % loop to import mutliple excel files
if i<10
data1= csvread(['Trial0' num2str(i) '.CSV'],5,4);
else i>10
data1= csvread(['Trial1' num2str(i) '.CSV'],5,4);
end
end
this is the error I receive:
Error using csvread (line 35). File not found.
Error in testcsvv (line 21)
data1= csvread(['Trial1'
num2str(i) '.CSV'],5,4);
It can't find the trial as it is called Trial01 however I thought I had wrote a code which would import Tria01 to 09 then from 10 on.
Where am I going wrong?
0 commentaires
Réponses (1)
dpb
le 16 Mai 2014
Modifié(e) : dpb
le 16 Mai 2014
Franchesca--
Please format your code more cleanly -- eliminate all the blank lines or use a % comment if think blank line is desperately needed so the formatting doesn't go in and out of "CODE" mode.
The easiest way I've found to enter code in the sorry text box (sorry, TMW, but it is just silly to use word wrap as the default for a coding forum as I've often noted before) is to paste the code and then put two blanks in front of the first line on a new paragraph. A paragraph is started with ONE blank line. Look at the preview window and keep fiddling until it reads cleanly there; until then your posting isn't ready for submittal.
As for your question, your loop line is
for i=10:numfiles;
not
for i=1:numfiles;
Hence your loop only operates over 10, 11, 12 excepting for one big problem. As you're written the logic, there's no branch to be executed for i==10 as you test for "less than" and "greater than" but never "equal". Hence nothing happens for i==10 and for i==11 the file to attempted to be opened will be--
>> i=11; ['Trial1' num2str(i) '.CSV']
ans =
Trial111.CSV
NB: here your formatting creates the numeric string '111', not '11' because you inserted a hardcoded '1' in front of the converted number location and the loop operates over the value so num2str inserts the '11'.
You could, of course, fix all these logic problems and implement it in this manner but you're going to a lot more effort than needed, however --
numfiles = 12;
data = cell(numfiles,1);
for i=1:numfiles
fname=sprintf('Trial%02d.CSV',i); % build a file name based on loop index
% debugging statement while testing follows--comment out when sure it's ok.
disp(['Preparing to read file: ' fname])
% check if the file exists on search path first..
if exist(fname,'file')~=2, error(['File not found on path: ' fname]),end
data1= csvread(fname,5,4);
end
NB above I presumed as you wrote the file names are 2-digit in width with a leading zero. Adjust the format string to match the actual naming convention if that changes. To generalize, simply fix up the loop indices to cover the range(s) wanted; don't have to be necessarily sequential, even.
BTW, as written you're going to overwrite data1 every pass thru the loop.
0 commentaires
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!