"Error using cell2struct Number of field names must match number of fields in new structure. Error in matrix_from_rasters (line 9) hdr = cell2struc​t(num2cell​(hdr{2}),h​dr{1});"

9 views (last 30 days)
Hi everybody! I'm trying to crate a column matrix from an ascii file having several rows and columns.
str = fileread('crosscurv_7x7_MAC.asc');
str = strrep(str,',','.');
% Parse Header:
opt = {'MultipleDelimsAsOne',true};
hdr = textscan(str,'%s%f',6,opt{:});
hdr = cell2struct(num2cell(hdr{2}),hdr{1});
% Parse Matrix:
opt = {'HeaderLines',numel(fieldnames(hdr)), 'CollectOutput',true};
fmt = repmat('%f32',1,hdr.ncols);
Crosscurv = textscan(str,fmt,opt{:});
Crosscurv = Crosscurv{1};
Crosscurv(Crosscurv==hdr.NODATA_value) = NaN;
Something is going wrong. I get this error:
"Error using cell2struct
Number of field names must match number of fields in new structure.
Error in matrix_from_rasters (line 9) hdr = cell2struct(num2cell(hdr{2}),hdr{1});"
Does anybody help me please?
Thanks in advance

Answers (1)

Jan
Jan on 22 May 2019
Use the debugger to examine, whats going on. Either set a breakpoint in the failing line, or stop Matlab automatically if an error occurs:
dbstop if error
Then run the code again and check, what the variables are:
% Failing line:
% hdr = cell2struct(num2cell(hdr{2}),hdr{1});
hdr{1}
hdr{2}
tmp = num2cell(hdr{1})
size(tmp)
size(hdr{1})
Then "Number of field names must match number of fields in new structure" shoudl be clear.

Categories

Find more on Cell Arrays 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!