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

조회 수: 1 (최근 30일)
Ryan
Ryan 2021년 8월 5일
댓글: Ryan 2021년 8월 5일
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})

채택된 답변

Rik
Rik 2021년 8월 5일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by