Dimensioning a field within a structure array for reading data

I am reading a file that contains a number of groups of fields and I'm trying to figure out how to do this.
I'd like to create structure (CTW) where I will group the data, but I'm running into problems defining and allocating the fields.
The first set of data in the file is an array of four variables of 8 characters each. I want to call these c8(1), through c8(4).
The second set of data in the file is 6 variables of 20 characters each. I want to call these c20(1), through c20(6).
Currently, my code looks like this:
fid = fopen(filename, 'r');
for x = 1 : 4
CTW.c8(x) = string(fread(fid, 8, '*char')');
end
for x = 1 : 6
CTW.c20(x) = string(fread(fid, 20, '*char')');
end
Matlab gives the following error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
I know that I can read in the individual strings and then create the structure, like this:
c81 = string(fread(fid, 8, '*char')');
c82 = string(fread(fid, 8, '*char')');
c83 = string(fread(fid, 8, '*char')');
c84 = string(fread(fid, 8, '*char')');
CTW.c8 = [c81, c82, c83, c84];
clear c81 c82 c83 c84
end
but that seems inelegant. What is the best way to do this? Is there some way to pre-allocate the arrays within the structure?

1 Comment

It appears that I can make this work in a slightly cumbersome way by predefining each set of variables like this:
fid = fopen(filename, 'r');
numFields = 4;
CTW.c8 = strings(numFields, 1);
for x = 1 : numFields
CTW.c8(x) = string(fread(fid, 8, '*char')');
end
numFields = 6;
CTW.c20 = strings(numFields, 1);
for x = 1 : numFields
CTW.c20(x) = fread(fid, 20, '*char');
end
I still suspect there's a better way to do this.

Sign in to comment.

 Accepted Answer

It might be worthwhile to use the readcell function to read it.
After that, the cell2struct function could work to create a structure from it with the characteristics you want.
I am not certain how easy that might be. I have used both of these funcitons (although rarely), however I have never done what you describe.
EDIT -- (18 ,Aug 2025 at 12:07)
One way to troubleshoot this would be to read the lines in separately to see what the problem is, and then make the appropriate changes to your code.
Example --
fid = fopen(filename, 'r');
for x = 1 : 4
c8str = string(fread(fid, 8, '*char')')
CTW.c8(x) = string(fread(fid, 8, '*char')');
end
for x = 1 : 6
c20str = string(fread(fid, 20, '*char')')
CTW.c20(x) = string(fread(fid, 20, '*char')');
end
fclose(fid);
That should reveal the reason the incompatibilities exist.
Importing the lines as cell arrays could also work. That might be as simple as enclosing them in curly braces:
fid = fopen(filename, 'r');
for x = 1 : 4
CTW.c8(x) = {string(fread(fid, 8, '*char')')};
end
for x = 1 : 6
CTW.c20(x) = {string(fread(fid, 20, '*char')')};
end
fclose(fid);
.

3 Comments

I'm not sure that readcell is going to work here. This is a binary file and it has a pretty messy structure. for example, the file begins like this:
  • two int16 fields (#lines, and #points)
  • a float32 field
  • a number of float32 fields equal to #lines
  • a 128 char comment field
  • a table of uint16 containing (#lines * #points) fields
  • the four 8-character fields I mentioned above
  • the six 20-character fields I mentioned above
  • etc.
So I'm pretty mich having to build my reader from scratch.
Your second answer seems to be the way forward. I guess I need a serious primer on the use of curly braces ;-) It works as a cell arrray of strings, or as a cell array of char arrays (below). So That's the way I'm going to go. Thank you!
fid = fopen(filename, 'r');
for x = 1 : 4
CTW.c8(x) = {fread(fid, 8, '*char')'};
end
for x = 1 : 6
CTW.c20(x) = {fread(fid, 20, '*char')'};
end
fclose(fid);
As always, my pleasure!
EDIT -- (18 Aug 2025 at 13:45)
Expanding on that, curly braces create a cell array. Cell arrays can hold different clases of variables, as well as different sizes of the same variable. It is usually straightforward to use them, however it may be necessary to use cellfun to deal with them efficiently.
.

Sign in to comment.

More Answers (1)

Your loop code has a problem when you reach end of file. In such a case, fread() returns empty, and string(empty) returns a 0 x 0 string, which cannot be stored in a 1 x 1 destination.

1 Comment

The problem is occurring in the very first iteration of the first loop, long before I reach the end of the file. As I said, I can read data using the other method, so there's data there, but for some reason the allocation isn't working.

Sign in to comment.

Products

Release

R2025a

Community Treasure Hunt

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

Start Hunting!