How to reorganize this cell array obtained from a fixed-field TXT file readout? How to adress lines in this cell array correctly?

1 view (last 30 days)
The following code produces me from a fixed-field TXT file a cell array of 5 columns of the correct widths, but instead of receiving my data sets in multiple rows (in a cell array of “NumberOfDataSets x 5 columns”), I unfortunately receive it as “1 row only x 5 columns” with each of the cells then individually containing the multiple lines standing for the multiple data sets. How to obtain the readout as wished as a “multiple line x 5 column” array?
filename=test.txt;
format='%3c%4c%1c%5c%3c';
fid=fopen(filename);
MyCellArray = textscan(fid,format, 'whitespace', '');
fclose(fid);
I in fact as an alternative did program already a fully working code made up by a loop using inside the fgetl(fid) function followed by some commands to shuffle the read data to the correct cells afterwards. But I so far failed to get it right away done with the textscan or fscanf functions and their various options. As I just can´t believe that MATLAB wouldn´t provide any comfortable function for the readout of a fixed-field TXT file, please help me to learn how to in general best deal with fixed-field data source problems, using simply the commands MATLAB already provides for this, and please do not just bypass me to a super quick performing PERL alternative ;-) . I am more searching for the correct format specification of the textscan or fscanf functions, or for a very simple reorganization command to operate on the already by now retrieved cell array from the code posted above. Thanks for teaching me to do things the “basic” MATLAB way!
  1 Comment
Marco
Marco on 23 Jan 2014
Edited: Marco on 23 Jan 2014
I find that the code above produces a “1” x “5” cell array, each cell containing data organized in "lines" x "amount of characters specified in the format". I furthermore find that its content is in general accessible as:
MyCellArray{CellArrayRow,CellArrayColumn}(line,digits)
This syntax extracts all {content} from the cell array row and cell array column position, and from all that content then takes the data found in the amount of digits in the specified line.
BUT when in combination with any (line,digits) statement the CellArrayColumn adresses a range of cell columns, instead of a single cell column only, then a "Bad cell reference operation" error returns.
MyCellArray{1,:} % works
MyCellArray{1,4}(1,1) % works
MyCellArray{1,4}(1,:) % works
MyCellArray{1,:}(1,1) % does not work
That’s all just difficult to understand for a beginner. Any help about how to extract an individual line across all columns without losing the fixed-field columns is welcome! I would like to extract the line MyCellArray{1,:}(line,:) but it obviously is not allowed to do by this syntax.

Sign in to comment.

Answers (0)

Categories

Find more on Large Files and Big Data 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!