importdata import only first part
Show older comments
I would like to use importdata to import my textfile. I have 20 headerlines and the file is tab delimited. But when I use this code:
delimiterIn ='\t' ;
column = 20;
array{U} = importdata(FileName,delimiterIn, column);
I only get a cell array.
What did I do wrong?
7 Comments
Jan
on 16 May 2018
What is U? What is the contents of the cell array you get? "column" is a strange name for the number of header lines.
Eliah Verbeemen
on 16 May 2018
Image Analyst
on 16 May 2018
Can you attach the text file? Can you use dlmread() instead?
dpb
on 16 May 2018
"only get a cell array. What did I do wrong?"
Perhaps nothing strictly, but the reason for a cell array is you wrote
array{U} = ...
so presuming U is some allowable integer value, you just returned all the content from the file returned by importdata and stored it at that location in a cell array because that's what the curlies "{}" mean.
If you instead are expecting/desire an "ordinary" array, just lose the subscript and write
array = importdata(...
and all (may) be well, depending of course on what the content of the file is and whether importdata can interpret it correctly without additional help.
@Eliah Verbeemen: "U is used for a loop" You would do me a favor, if you simply post, what U is. U=1 would be sufficient already. Typing "used for a loop" takes longer and leave space for interpretations. Thanks.
"Is the name important?" - If you post code in a forum and want others to help you, using confusing names for variables is a bad idea. But using misleading names is a bad idea in general, and it matters in fact, when you debug larger codes.
Again: What is the contents of the cell array you get? Currently it is not clear, why you assume, that this is an error at all.
Eliah Verbeemen
on 16 May 2018
Edited: dpb
on 16 May 2018
dpb
on 16 May 2018
Don't take "shortcuts"; cut 'n paste *precisely and identically the code itself"; yest details DO matter.
In general it will be well rewarded over the long haul if you impose discipline when writing code such that variable names do tend to have meanings related to their purpose; trying to reuse names for multiple purposes or to not create another variable name for a purpose is false economy; the confusion raised far out weighs what miniscule amount of memory might be saved.
As for the question of the cell array, if you're going to be importing multiple files such as this, unless you can ensure that every file has the identical number of variables/size, then storing those as arrays in a cell array is one of the fairly limited number of choices you have; a 3D array with each file being a plane of 2D can only exist if each is identical in size; you could concatenate the same number of variables but wouldn't then know which part of the final result came from which file (which may or may not be important)
All in all, without example data files and just what you're needing to do in the end, it's all just a guessing game.
Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!