How to get consistent import data outputs

Im my program I am attempting to import multiple xcel files in a for loop. Then storing the results in a struct with dynamically named fields which I can evaluate later. However, when the for loop is done I get what is in the screenshot below the code.
for i=1:sizeOfSel
j = num2str(i);
fieldName = strcat('data_' , j);
stmfile = temp(i,:)
pathname = handles.A.dir;
tic
handles.A.data.(fieldName) = importdata(fullfile(pathname, stmfile));
toc
end
Ideally, I need every field to contain a structure that import data is outputting. But because only one or two of the files being processed actually fit the format, I can only process those files. The image shows the output of two identical xcel files.

 Accepted Answer

They are both xls. I copy/pasted it, so besides an extra '-copy' in the name they should contain the same values.

4 Comments

Is that a copy-paste of the file, or of the contents?
If you rename the copy so that it matches the pattern but would occur earlier in the list than the file that works now, then does the problem stay with the copy or does it stay with the slot, the fact that it is not the first file?
copy-paste of the file itself.
So breaking the problem down I've figured out it's because I'm calling specific file names that don't follow a pattern. Right now each iteration through the loop the exact filename/path is handed to import data.
Is there a way to import them individually without importdata/xls reader expecting a pattern?
The only thing about pattern that importdata() cares about is that it looks at the file extension to figure out how to handle the file. If your copy ended up without the .xls extension then it would have been treated as ASCII data. There is no provision to override that behaviour to tell it which helper to call. If you need to have it handle filenames with unexpected extension as if they were a particular type, then you should use the appropriate helper function such as xlsread()
I'm able to watch the incoming filenames as they are processed and I haven't lost any extensions. If I have files 1.xls 2.xls and 3.xls it will happily process all files. But if I change 3.xls to a3.xls or any change outside of changing 3 to (another single digit number).xls it will no longer be able to process all three files.
Going to have to restructure some things but it looks like xlsread will be the way to go.
Thank you!

Sign in to comment.

More Answers (1)

Are they really identical, or have some of them been saved in csv (Comma Separated Value) format, and some of them in binary format (.xls)? And possibly some of them are in .xlsx structured text format?
You will probably need to switch to xlsread() instead of importdata()

Products

Asked:

on 29 Jun 2015

Commented:

on 29 Jun 2015

Community Treasure Hunt

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

Start Hunting!