Error: Brace indexing is not supported for variables of this type.

1 view (last 30 days)
I am attempting to read in a txt file. However, I keep getting a brace indexing error. How is this resolved?
>> filename=('Read_In_1.txt');
fileID = fopen(filename);
% Example data in file
% UI = "555"
% SDD = "133"
% SOD = "200"
% Read data into a table, delimiter being a space
T = readtable(filename,'Delimiter',' ');
[r,c] = size(T);
% With example data, table will be 3x3
% Column 1, T.Var1 containing variable names
% Column 2, T.Var2 containing '=' for all rows
% Column 3, T.Var3 containing variable values as text strings
% only interested in column 1 & column 3 data
% Assign a variable with name from T.Var1{ii} the numerical value
% derived from T.Var3(ii)
for ii = 1:r
assignin('base',T.Var1{ii},str2double(cell2mat(T.Var3(ii))));
end
% Show what's in workspace now
whos
fclose(fileID);
return
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})

Accepted Answer

Rik
Rik on 5 Aug 2021
Why are you reading your file like that? And why are you using assignin? There is no reason to be using that here.
Since you're using R2021a I would suggest using readlines to read your text file.
txt=readlines('foo.txt');
[field,value]=arrayfun(@customFunction,txt);
data=struct;
for n=1:numel(field)
data.(field(n))=value(n);
end
data
data = struct with fields:
UI: 555 SDD: 133 SOD: 200
function [f,v]=customFunction(str)
str=split(str,'=');
f=strrep(deblank(str(1)),'"','');
v=str2double(strrep(deblank(str(2)),'"',''));
end

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!