Brace indexing is not supported for this type of variable

4 次查看(过去 30 天)
Brace indexing not supported for this type of variable - error message, attached code, please support in resolving this issue
global new_files_all
[load_file, ~] = uigetfile('.mat','File load','MultiSelect', 'on');
new_files_all = load_file;
old_sname=extractBefore(load_file(1),'_');
for i=1:length(load_file)
sname = extractBefore(load_file{i},'_');
if i==1
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
old_sname = sname;
elseif (i>=2)
if snumber == 1 && strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
elseif snumber >= 1 && strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber;
old_sname = sname;
elseif snumber >= 1 && ~strcmp(old_sname, sname)
load(load_file{i});
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber+1;
old_sname = sname;
end
end
new_files = {load_file{i}};
  4 个评论
Kavinprasad M
Kavinprasad M 2023-6-20
Yeah i can see the issue but my file will not have a variable named load_file since it's being directly exported from a data source i have externally which will only have certain input variable names
Stephen23
Stephen23 2023-6-20
编辑:Stephen23 2023-6-20
@Kavinprasad M: it is worth getting into the habit of writing more robust code, as Steven Lord recommends: the more you write it, the easier it becomes. And although you state that you are "certain" that the source only uses certain variable names, we get many many questions on this forum where the OP is "certain" of one thing or another... which turn out to be not true. The advice Steven Lord gave helps when the data is not as expected, as well as improving efficiency during its nominal/expected operation. Making code work is great... now you can upgrade your skills by controlling under what circumstances it will work and how it behaves when something unexpected happens. It will likely be more efficient to boot.
I second Steven Lord's recommendation: knowing how to write robust MATLAB code is definitely worth learning.

请先登录,再进行评论。

回答(2 个)

Ayush Kashyap
Ayush Kashyap 2023-6-19
Error message like this one: "Brace indexing not supported for this type of variable" appears mainly when curly braces "{}" are used for indexing instead of parantheses "()".
Looking at your code, I believe the issue is happeneing because of lines like this one:
sname = extractBefore(load_file{i},'_');
Try using parantheses as follows:
sname = extractBefore(load_file(i),'_');
Similarly, try to check your whole code once and replace all instances where you have used "{}" for indexing with "()".
Reference Link: Indexing in Matlab

Image Analyst
Image Analyst 2023-6-19
I think this is closer to what you want. It has many improvements. I'm not sure what you want with that sname stuff so there still may be errors.
global new_files_all
% Get a file specification.
folder = pwd; % Wherever you want.
filePattern = fullfile(folder, '*.mat');
% Ask user to select files.
[baseFileNames, folder] = uigetfile(filePattern,'File load','MultiSelect', 'on');
if folder == 0
fprintf('You clicked Cancel.\n');
return; % User clicked cancel.
end
numberOfFiles = numel(baseFileNames)
new_files_all = cell(numberOfFiles, 1);
old_sname=extractBefore(folder(1),'_'); % Not sure what this is for.
for k = 1 : numberOfFiles
thisFileName = fullfile(folder, baseFileNames{k});
fprintf('Processing %s (#%d of %d)\n', thisFileName, k, numberOfFiles);
sname = extractBefore(thisFileName,'_');
if k==1
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
old_sname = sname;
elseif (k>=2)
if snumber == 1 && strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=1;
elseif snumber >= 1 && strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber;
old_sname = sname;
elseif snumber >= 1 && ~strcmp(old_sname, sname)
load(thisFileName);
time=shdf.Absc1Data;
noise=shdf.Data(1,:);
rpm=shdf.Data(2,:);
snumber=snumber+1;
old_sname = sname;
end
end
new_files{k} = thisFileName;
end
  1 个评论
Kavinprasad M
Kavinprasad M 2023-6-20
编辑:Kavinprasad M 2023-6-20
Thankyou, this code is better but it still throws the same error for baseFileNames{k}
Attached complete application

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by