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.

 채택된 답변

Tim Tobias
Tim Tobias 2015년 6월 29일

0 개 추천

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

댓글 수: 4

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?
Tim Tobias
Tim Tobias 2015년 6월 29일
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()
Tim Tobias
Tim Tobias 2015년 6월 29일
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!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 29일

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()

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

질문:

2015년 6월 29일

댓글:

2015년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by