How to get consistent import data outputs

3 views (last 30 days)
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

Tim Tobias
Tim Tobias on 29 Jun 2015
They are both xls. I copy/pasted it, so besides an extra '-copy' in the name they should contain the same values.
  4 Comments
Walter Roberson
Walter Roberson on 29 Jun 2015
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()
Tim Tobias
Tim Tobias on 29 Jun 2015
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)

Walter Roberson
Walter Roberson on 29 Jun 2015
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()

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!