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 Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by